<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="Mybatis配置说明, 夏天">
    <meta name="description" content="爱一直在线">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>Mybatis配置说明 | 夏天</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery.min.js"></script>
    <script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script>
<style type="text/css" lang="css">
    #loading-container{
        position: fixed;
        top: 0;
        left: 0;
        min-height: 100vh;
        width: 100vw;
        z-index: 9999;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        background: #FFF;
        text-align: center;
        /* loader页面消失采用渐隐的方式*/
        -webkit-transition: opacity 1s ease;
        -moz-transition: opacity 1s ease;
        -o-transition: opacity 1s ease;
        transition: opacity 1s ease;
    }
    .loading-image{
        width: 120px;
        height: 50px;
        transform: translate(-50%);
    }

    .loading-image div:nth-child(2) {
        -webkit-animation: pacman-balls 1s linear 0s infinite;
        animation: pacman-balls 1s linear 0s infinite
    }

    .loading-image div:nth-child(3) {
        -webkit-animation: pacman-balls 1s linear .33s infinite;
        animation: pacman-balls 1s linear .33s infinite
    }

    .loading-image div:nth-child(4) {
        -webkit-animation: pacman-balls 1s linear .66s infinite;
        animation: pacman-balls 1s linear .66s infinite
    }

    .loading-image div:nth-child(5) {
        -webkit-animation: pacman-balls 1s linear .99s infinite;
        animation: pacman-balls 1s linear .99s infinite
    }

   .loading-image div:first-of-type {
        width: 0;
        height: 0;
        border: 25px solid #49b1f5;
        border-right-color: transparent;
        border-radius: 25px;
        -webkit-animation: rotate_pacman_half_up .5s 0s infinite;
        animation: rotate_pacman_half_up .5s 0s infinite;
    }
    .loading-image div:nth-child(2) {
        width: 0;
        height: 0;
        border: 25px solid #49b1f5;
        border-right-color: transparent;
        border-radius: 25px;
        -webkit-animation: rotate_pacman_half_down .5s 0s infinite;
        animation: rotate_pacman_half_down .5s 0s infinite;
        margin-top: -50px;
    }
    @-webkit-keyframes rotate_pacman_half_up {0% {transform: rotate(270deg)}50% {transform: rotate(1turn)}to {transform: rotate(270deg)}}

    @keyframes rotate_pacman_half_up {0% {transform: rotate(270deg)}50% {transform: rotate(1turn)}to {transform: rotate(270deg)}}

    @-webkit-keyframes rotate_pacman_half_down {0% {transform: rotate(90deg)}50% {transform: rotate(0deg)}to {transform: rotate(90deg)}}

    @keyframes rotate_pacman_half_down {0% {transform: rotate(90deg)}50% {transform: rotate(0deg)}to {transform: rotate(90deg)}}

    @-webkit-keyframes pacman-balls {75% {opacity: .7}to {transform: translate(-100px, -6.25px)}}

    @keyframes pacman-balls {75% {opacity: .7}to {transform: translate(-100px, -6.25px)}}


    .loading-image div:nth-child(3),
    .loading-image div:nth-child(4),
    .loading-image div:nth-child(5),
    .loading-image div:nth-child(6){
        background-color: #49b1f5;
        width: 15px;
        height: 15px;
        border-radius: 100%;
        margin: 2px;
        width: 10px;
        height: 10px;
        position: absolute;
        transform: translateY(-6.25px);
        top: 25px;
        left: 100px;
    }
    .loading-text{
        margin-bottom: 20vh;
        text-align: center;
        color: #2c3e50;
        font-size: 2rem;
        box-sizing: border-box;
        padding: 0 10px;
        text-shadow: 0 2px 10px rgba(0,0,0,0.2);
    }
    @media only screen and (max-width: 500px) {
         .loading-text{
            font-size: 1.5rem;
         }
    }
    .fadeout {
        opacity: 0;
        filter: alpha(opacity=0);
    }
    /* logo出现动画 */
    @-webkit-keyframes fadeInDown{0%{opacity:0;-webkit-transform:translate3d(0,-100%,0);transform:translate3d(0,-100%,0)}100%{opacity:1;-webkit-transform:none;transform:none}}
    @keyframes fadeInDown{0%{opacity:0;-webkit-transform:translate3d(0,-100%,0);}}
 </style>
 <script>
(function () {
    const loaded = function(){
       setTimeout(function(){
            const loader = document.getElementById("loading-container");
            loader.className="fadeout" ;//使用渐隐的方法淡出loading page
            // document.getElementById("body-wrap").style.display="flex";
            setTimeout(function(){
                loader.style.display="none";
            },1000); 
        },1000);//强制显示loading page 1s  
    };
    loaded();
})()
 </script><meta name="generator" content="Hexo 4.2.0"><link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"><style type="text/css" lang="css">
    #loading-container{
        position: fixed;
        top: 0;
        left: 0;
        min-height: 100vh;
        width: 100vw;
        z-index: 9999;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        background: #FFF;
        text-align: center;
        /* loader页面消失采用渐隐的方式*/
        -webkit-transition: opacity 1s ease;
        -moz-transition: opacity 1s ease;
        -o-transition: opacity 1s ease;
        transition: opacity 1s ease;
    }
    .loading-image{
        width: 120px;
        height: 50px;
        transform: translate(-50%);
    }

    .loading-image div:nth-child(2) {
        -webkit-animation: pacman-balls 1s linear 0s infinite;
        animation: pacman-balls 1s linear 0s infinite
    }

    .loading-image div:nth-child(3) {
        -webkit-animation: pacman-balls 1s linear .33s infinite;
        animation: pacman-balls 1s linear .33s infinite
    }

    .loading-image div:nth-child(4) {
        -webkit-animation: pacman-balls 1s linear .66s infinite;
        animation: pacman-balls 1s linear .66s infinite
    }

    .loading-image div:nth-child(5) {
        -webkit-animation: pacman-balls 1s linear .99s infinite;
        animation: pacman-balls 1s linear .99s infinite
    }

   .loading-image div:first-of-type {
        width: 0;
        height: 0;
        border: 25px solid #49b1f5;
        border-right-color: transparent;
        border-radius: 25px;
        -webkit-animation: rotate_pacman_half_up .5s 0s infinite;
        animation: rotate_pacman_half_up .5s 0s infinite;
    }
    .loading-image div:nth-child(2) {
        width: 0;
        height: 0;
        border: 25px solid #49b1f5;
        border-right-color: transparent;
        border-radius: 25px;
        -webkit-animation: rotate_pacman_half_down .5s 0s infinite;
        animation: rotate_pacman_half_down .5s 0s infinite;
        margin-top: -50px;
    }
    @-webkit-keyframes rotate_pacman_half_up {0% {transform: rotate(270deg)}50% {transform: rotate(1turn)}to {transform: rotate(270deg)}}

    @keyframes rotate_pacman_half_up {0% {transform: rotate(270deg)}50% {transform: rotate(1turn)}to {transform: rotate(270deg)}}

    @-webkit-keyframes rotate_pacman_half_down {0% {transform: rotate(90deg)}50% {transform: rotate(0deg)}to {transform: rotate(90deg)}}

    @keyframes rotate_pacman_half_down {0% {transform: rotate(90deg)}50% {transform: rotate(0deg)}to {transform: rotate(90deg)}}

    @-webkit-keyframes pacman-balls {75% {opacity: .7}to {transform: translate(-100px, -6.25px)}}

    @keyframes pacman-balls {75% {opacity: .7}to {transform: translate(-100px, -6.25px)}}


    .loading-image div:nth-child(3),
    .loading-image div:nth-child(4),
    .loading-image div:nth-child(5),
    .loading-image div:nth-child(6){
        background-color: #49b1f5;
        width: 15px;
        height: 15px;
        border-radius: 100%;
        margin: 2px;
        width: 10px;
        height: 10px;
        position: absolute;
        transform: translateY(-6.25px);
        top: 25px;
        left: 100px;
    }
    .loading-text{
        margin-bottom: 20vh;
        text-align: center;
        color: #2c3e50;
        font-size: 2rem;
        box-sizing: border-box;
        padding: 0 10px;
        text-shadow: 0 2px 10px rgba(0,0,0,0.2);
    }
    @media only screen and (max-width: 500px) {
         .loading-text{
            font-size: 1.5rem;
         }
    }
    .fadeout {
        opacity: 0;
        filter: alpha(opacity=0);
    }
    /* logo出现动画 */
    @-webkit-keyframes fadeInDown{0%{opacity:0;-webkit-transform:translate3d(0,-100%,0);transform:translate3d(0,-100%,0)}100%{opacity:1;-webkit-transform:none;transform:none}}
    @keyframes fadeInDown{0%{opacity:0;-webkit-transform:translate3d(0,-100%,0);}}
 </style>
 <script>
(function () {
    const loaded = function(){
       setTimeout(function(){
            const loader = document.getElementById("loading-container");
            loader.className="fadeout" ;//使用渐隐的方法淡出loading page
            // document.getElementById("body-wrap").style.display="flex";
            setTimeout(function(){
                loader.style.display="none";
            },1000); 
        },1000);//强制显示loading page 1s  
    };
    loaded();
})()
 </script></head>



 <div id="loading-container">
     <p class="loading-text">玩命加载中 . . . </p> 
     <div class="loading-image">
         <div></div>
         <div></div>
         <div></div>
         <div></div> 
         <div></div>
     </div>
 </div><body>    
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">夏天</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>主页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <span>百宝箱</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="https://caimengzhi.gitee.io/webstack/cn/index.html">
          
          <i class="fas fa-briefcase" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>宝箱一</span>
        </a>
      </li>
      
      <li>
        <a href="https://caimengzhi.gitee.io/nav/">
          
          <i class="fas fa-briefcase" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>宝箱二</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fas fa-sitemap" style="zoom: 0.6;"></i>
      
      <span>统计</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/tags">
          
          <i class="fas fa-tags" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>标签</span>
        </a>
      </li>
      
      <li>
        <a href="/categories">
          
          <i class="fas fa-bookmark" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>分类</span>
        </a>
      </li>
      
      <li>
        <a href="/archives">
          
          <i class="fas fa-archive" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>归档</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fa fa-gamepad" style="zoom: 0.6;"></i>
      
      <span>游戏</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="https://caimengzhi.gitee.io/magiccube/">
          
          <i class="fa fa-money" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>魔方</span>
        </a>
      </li>
      
      <li>
        <a href="https://caimengzhi.gitee.io/game-2048">
          
          <i class="fa fa-money" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>2048</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fas fa-list" style="zoom: 0.6;"></i>
      
      <span>知识库</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="https://caimengzhi.gitee.io/java/">
          
          <i class="fas fa-music" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>JAVA</span>
        </a>
      </li>
      
      <li>
        <a href="https://caimengzhi.gitee.io/mysql/">
          
          <i class="fas fa-film" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>MySQL</span>
        </a>
      </li>
      
      <li>
        <a href="https://caimengzhi.gitee.io/nosql/">
          
          <i class="fas fa-book" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>NoSQL</span>
        </a>
      </li>
      
      <li>
        <a href="https://caimengzhi.gitee.io/oracle/">
          
          <i class="fas fa-image" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>oracle</span>
        </a>
      </li>
      
      <li>
        <a href="https://caimengzhi.gitee.io/structure">
          
          <i class="fas fa-image" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>算法</span>
        </a>
      </li>
      
      <li>
        <a href="https://caimengzhi.gitee.io/hadoop/">
          
          <i class="fas fa-image" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>大数据</span>
        </a>
      </li>
      
      <li>
        <a href="https://caimengzhi.gitee.io/docker">
          
          <i class="fas fa-image" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>K8S</span>
        </a>
      </li>
      
      <li>
        <a href="https://caimengzhi.gitee.io/frontend/">
          
          <i class="fas fa-image" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>前端</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <span>联系</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/contact">
          
          <i class="fas fa-comments" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>联系</span>
        </a>
      </li>
      
      <li>
        <a href="/about">
          
          <i class="fas fa-user-circle" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>关于</span>
        </a>
      </li>
      
      <li>
        <a href="/friends">
          
          <i class="fas fa-address-book" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>友链</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="https://news.qq.com/zt2020/page/feiyan.htm#/?nojump=1" target="_blank" rel="noopener" class="waves-effect waves-light">
      
      <i class="fas fa-heartbeat" style="zoom: 0.6;"></i>
      
      <span>疫情</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">夏天</div>
        <div class="logo-desc">
            
            爱一直在线
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			主页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fas fa-fw fa-link"></i>
			
			百宝箱
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>   
				
                  <a href="https://caimengzhi.gitee.io/webstack/cn/index.html " style="margin-left:50px";>
				  
				   <i class="fa fas fa-briefcase" style="position: absolute;left:28px" ></i>
			      
		          <span>宝箱一</span>
                  </a>
                </li>
              
                <li>   
				
                  <a href="https://caimengzhi.gitee.io/nav/ " style="margin-left:50px";>
				  
				   <i class="fa fas fa-briefcase" style="position: absolute;left:28px" ></i>
			      
		          <span>宝箱二</span>
                  </a>
                </li>
               
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-sitemap"></i>
			
			统计
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>   
				
                  <a href="/tags " style="margin-left:50px";>
				  
				   <i class="fa fas fa-tags" style="position: absolute;left:28px" ></i>
			      
		          <span>标签</span>
                  </a>
                </li>
              
                <li>   
				
                  <a href="/categories " style="margin-left:50px";>
				  
				   <i class="fa fas fa-bookmark" style="position: absolute;left:28px" ></i>
			      
		          <span>分类</span>
                  </a>
                </li>
              
                <li>   
				
                  <a href="/archives " style="margin-left:50px";>
				  
				   <i class="fa fas fa-archive" style="position: absolute;left:28px" ></i>
			      
		          <span>归档</span>
                  </a>
                </li>
               
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fa fa-gamepad"></i>
			
			游戏
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>   
				
                  <a href="https://caimengzhi.gitee.io/magiccube/ " style="margin-left:50px";>
				  
				   <i class="fa fa fa-money" style="position: absolute;left:28px" ></i>
			      
		          <span>魔方</span>
                  </a>
                </li>
              
                <li>   
				
                  <a href="https://caimengzhi.gitee.io/game-2048 " style="margin-left:50px";>
				  
				   <i class="fa fa fa-money" style="position: absolute;left:28px" ></i>
			      
		          <span>2048</span>
                  </a>
                </li>
               
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-list"></i>
			
			知识库
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>   
				
                  <a href="https://caimengzhi.gitee.io/java/ " style="margin-left:50px";>
				  
				   <i class="fa fas fa-music" style="position: absolute;left:28px" ></i>
			      
		          <span>JAVA</span>
                  </a>
                </li>
              
                <li>   
				
                  <a href="https://caimengzhi.gitee.io/mysql/ " style="margin-left:50px";>
				  
				   <i class="fa fas fa-film" style="position: absolute;left:28px" ></i>
			      
		          <span>MySQL</span>
                  </a>
                </li>
              
                <li>   
				
                  <a href="https://caimengzhi.gitee.io/nosql/ " style="margin-left:50px";>
				  
				   <i class="fa fas fa-book" style="position: absolute;left:28px" ></i>
			      
		          <span>NoSQL</span>
                  </a>
                </li>
              
                <li>   
				
                  <a href="https://caimengzhi.gitee.io/oracle/ " style="margin-left:50px";>
				  
				   <i class="fa fas fa-image" style="position: absolute;left:28px" ></i>
			      
		          <span>oracle</span>
                  </a>
                </li>
              
                <li>   
				
                  <a href="https://caimengzhi.gitee.io/structure " style="margin-left:50px";>
				  
				   <i class="fa fas fa-image" style="position: absolute;left:28px" ></i>
			      
		          <span>算法</span>
                  </a>
                </li>
              
                <li>   
				
                  <a href="https://caimengzhi.gitee.io/hadoop/ " style="margin-left:50px";>
				  
				   <i class="fa fas fa-image" style="position: absolute;left:28px" ></i>
			      
		          <span>大数据</span>
                  </a>
                </li>
              
                <li>   
				
                  <a href="https://caimengzhi.gitee.io/docker " style="margin-left:50px";>
				  
				   <i class="fa fas fa-image" style="position: absolute;left:28px" ></i>
			      
		          <span>K8S</span>
                  </a>
                </li>
              
                <li>   
				
                  <a href="https://caimengzhi.gitee.io/frontend/ " style="margin-left:50px";>
				  
				   <i class="fa fas fa-image" style="position: absolute;left:28px" ></i>
			      
		          <span>前端</span>
                  </a>
                </li>
               
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fas fa-fw fa-link"></i>
			
			联系
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>   
				
                  <a href="/contact " style="margin-left:50px";>
				  
				   <i class="fa fas fa-comments" style="position: absolute;left:28px" ></i>
			      
		          <span>联系</span>
                  </a>
                </li>
              
                <li>   
				
                  <a href="/about " style="margin-left:50px";>
				  
				   <i class="fa fas fa-user-circle" style="position: absolute;left:28px" ></i>
			      
		          <span>关于</span>
                  </a>
                </li>
              
                <li>   
				
                  <a href="/friends " style="margin-left:50px";>
				  
				   <i class="fa fas fa-address-book" style="position: absolute;left:28px" ></i>
			      
		          <span>友链</span>
                  </a>
                </li>
               
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="https://news.qq.com/zt2020/page/feiyan.htm#/?nojump=1" target="_blank" rel="noopener" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-heartbeat"></i>
			
			疫情
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://caimengzhi.gitee.io/books/" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>

        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://caimengzhi.gitee.io/books/" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    
<script src="/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('您好，BOSS,请输入密码')).toString(CryptoJS.enc.Hex)) {
                alert('哈哈密码错了！！！');
                location.href = '/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('http://mybatis.github.io/images/mybatis-logo.png')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">Mybatis配置说明</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        height: calc(100vh - 250px);
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/mybatis/">
                                <span class="chip bg-color">mybatis</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/mybatis/" class="post-category">
                                mybatis
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2020-04-10
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2020-04-18
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    14.2k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    70 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>

        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <center><h1>Mybatis的xml映射文件</h1></center>

<blockquote>
<p>本地所有code地址:  <a href="https://gitee.com/caimengzhi/code_java/tree/master/mybatis/mybatis_sqlmapping" target="_blank" rel="noopener">https://gitee.com/caimengzhi/code_java/tree/master/mybatis/mybatis_sqlmapping</a></p>
</blockquote>
<details>
  <summary>测试标题折叠</summary>
```
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

<pre><code>&lt;groupId&gt;com.cmz&lt;/groupId&gt;
&lt;artifactId&gt;mybatis_generator&lt;/artifactId&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;


&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.mybatis&lt;/groupId&gt;
        &lt;artifactId&gt;mybatis&lt;/artifactId&gt;
        &lt;version&gt;3.5.4&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;mysql&lt;/groupId&gt;
        &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
        &lt;version&gt;8.0.19&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;log4j&lt;/groupId&gt;
        &lt;artifactId&gt;log4j&lt;/artifactId&gt;
        &lt;version&gt;1.2.17&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;junit&lt;/groupId&gt;
        &lt;artifactId&gt;junit&lt;/artifactId&gt;
        &lt;version&gt;4.13&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;org.mybatis.caches&lt;/groupId&gt;
        &lt;artifactId&gt;mybatis-ehcache&lt;/artifactId&gt;
        &lt;version&gt;1.2.0&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
        &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
        &lt;version&gt;2.0.0-alpha1&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
        &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
        &lt;version&gt;2.0.0-alpha1&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;org.mybatis.generator&lt;/groupId&gt;
        &lt;artifactId&gt;mybatis-generator-core&lt;/artifactId&gt;
        &lt;version&gt;1.4.0&lt;/version&gt;
    &lt;/dependency&gt;

&lt;/dependencies&gt;</code></pre></project>
```
</details>
```

<h2 id="0-xml-映射文件"><a href="#0-xml-映射文件" class="headerlink" title="0. xml 映射文件"></a>0. xml 映射文件</h2><blockquote>
<p><a href="https://mybatis.org/mybatis-3/zh/getting-started.html" target="_blank" rel="noopener">https://mybatis.org/mybatis-3/zh/getting-started.html</a></p>
</blockquote>
<pre class=" language-xml"><code class="language-xml"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8" ?></span>
<span class="token doctype">&lt;!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>mapper</span> <span class="token attr-name">namespace</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.dao.EmpDao<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByEmpno<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span> <span class="token attr-name">databaseId</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>mysql<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> /* 没有别名之前*/
    <span class="token comment" spellcheck="true">&lt;!--&lt;select id="selectEmpByEmpno" resultType="Emp">   /*添加别名之后，可以简写*/--></span>
        select * from emp where empno = #{empno}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByEmpno<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span> <span class="token attr-name">databaseId</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>oracle<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        select * from emp where empno = #{empno}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>


    <span class="token comment" spellcheck="true">&lt;!--insert delete update分别用来标识不同类型的sql语句
    id: 用来标识跟dao接口中匹配的方法，必须与方法的名字一一对应。
    --></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>insert</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>save<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        insert into emp(empno,ename) values(#{empno},#{ename})
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>insert</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>update</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>update<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        update emp set sal=#{sal} where empno=#{empno}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>update</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>delete</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>delete<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        delete from emp where empno=#{empno}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>delete</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>mapper</span><span class="token punctuation">></span></span></code></pre>
<blockquote>
<p>其中 <code>id</code>,<code>flushCache</code>,<code>useGeneratedKeys</code>,<code>KeyProperty</code>相对需要掌握</p>
</blockquote>
<h2 id="1-Insert-Update-Delete-元素的属性"><a href="#1-Insert-Update-Delete-元素的属性" class="headerlink" title="1. Insert, Update, Delete 元素的属性"></a>1. Insert, Update, Delete 元素的属性</h2><table>
<thead>
<tr>
<th align="left">属性</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><code>id</code></td>
<td align="left">在命名空间中唯一的标识符，可以被用来引用这条语句。</td>
</tr>
<tr>
<td align="left"><code>parameterType</code></td>
<td align="left">将会传入这条语句的参数的类全限定名或别名。这个属性是可选的，因为 MyBatis 可以通过类型处理器（TypeHandler）推断出具体传入语句的参数，默认值为未设置（unset）。</td>
</tr>
<tr>
<td align="left"><code>parameterMap</code></td>
<td align="left">用于引用外部 parameterMap 的属性，目前已被废弃。请使用行内参数映射和 parameterType 属性。</td>
</tr>
<tr>
<td align="left"><code>flushCache</code></td>
<td align="left">将其设置为 true 后，只要语句被调用，都会导致本地缓存和二级缓存被清空，默认值：（对 insert、update 和 delete 语句）true。</td>
</tr>
<tr>
<td align="left"><code>timeout</code></td>
<td align="left">这个设置是在抛出异常之前，驱动程序等待数据库返回请求结果的秒数。默认值为未设置（unset）（依赖数据库驱动）。</td>
</tr>
<tr>
<td align="left"><code>statementType</code></td>
<td align="left">可选 STATEMENT，PREPARED 或 CALLABLE。这会让 MyBatis 分别使用 Statement，PreparedStatement 或 CallableStatement，默认值：PREPARED。</td>
</tr>
<tr>
<td align="left"><code>useGeneratedKeys</code></td>
<td align="left">（仅适用于 insert 和 update）这会令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键（比如：像 MySQL 和 SQL Server 这样的关系型数据库管理系统的自动递增字段），默认值：false。</td>
</tr>
<tr>
<td align="left"><code>keyProperty</code></td>
<td align="left">（仅适用于 insert 和 update）指定能够唯一识别对象的属性，MyBatis 会使用 getGeneratedKeys 的返回值或 insert 语句的 selectKey 子元素设置它的值，默认值：未设置（<code>unset</code>）。如果生成列不止一个，可以用逗号分隔多个属性名称。</td>
</tr>
<tr>
<td align="left"><code>keyColumn</code></td>
<td align="left">（仅适用于 insert 和 update）设置生成键值在表中的列名，在某些数据库（像 PostgreSQL）中，当主键列不是表中的第一列的时候，是必须设置的。如果生成列不止一个，可以用逗号分隔多个属性名称。</td>
</tr>
<tr>
<td align="left"><code>databaseId</code></td>
<td align="left">如果配置了数据库厂商标识（databaseIdProvider），MyBatis 会加载所有不带 databaseId 或匹配当前 databaseId 的语句；如果带和不带的语句都有，则不带的会被忽略。</td>
</tr>
</tbody></table>
<h3 id="1-1-id"><a href="#1-1-id" class="headerlink" title="1.1 id"></a>1.1 id</h3><p>用来标识跟dao接口中匹配的方法，必须与方法的名字一一对应。</p>
<p>EmpDao.class</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>dao<span class="token punctuation">;</span>

<span class="token keyword">import</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>bean<span class="token punctuation">.</span>Emp<span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/**
 * @author summer
 * @create 2020-04-08 16:35
 */</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">EmpDao</span> <span class="token punctuation">{</span>

    <span class="token keyword">public</span> Integer <span class="token function">save</span><span class="token punctuation">(</span>Emp emp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> Integer <span class="token function">update</span><span class="token punctuation">(</span>Emp emp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> Integer <span class="token function">delete</span><span class="token punctuation">(</span>Integer empno<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> Emp <span class="token function">selectEmpByEmpno</span><span class="token punctuation">(</span>Integer empno<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>xml中的id要和对应此接口的方法名字一样。</p>
<h3 id="1-2-statementType"><a href="#1-2-statementType" class="headerlink" title="1.2 statementType"></a>1.2 statementType</h3><p>用来选择执行sql语句的方式。</p>
<ul>
<li>statement: 最基本的jdbc操作，用来表示一个sql语句，不能防止sql注入</li>
<li>PREPARED: preareStatement: 采用预编译的方式，它能够防止sql注入,设计参数时候需要改对象来进行设置</li>
<li>CALLABLE: 调用存储过程</li>
</ul>
<h3 id="1-3-useGeneratedKeys"><a href="#1-3-useGeneratedKeys" class="headerlink" title="1.3 useGeneratedKeys"></a>1.3 useGeneratedKeys</h3><p>官方的说法是该参数的作用是：“允许JDBC支持自动生成主键，需要驱动兼容”，如何理解这句话的意思？</p>
<p>其本意是说：对于支持自动生成记录主键的数据库，如：MySQL，SQL Server，此时设置useGeneratedKeys参数值为true，在执行添加记录之后可以获取到数据库自动生成的主键ID。</p>
<p>&emsp;&emsp;当主键是自增的情况下，添加一条记录的同时，其主键是不能使用的，但是有时我们需要该主键，这时我们该如何处理呢？这时我们只需要在其对应xml中加入以下属性即可：</p>
<pre class=" language-xml"><code class="language-xml">useGeneratedKeys=“true” keyProperty=“id”
useGeneratedKeys设置为 true 时
1. 表示如果插入的表id以自增列为主键，则允许 JDBC 支持自动生成主键
2. 并可将自动生成的主键id返回。
useGeneratedKeys参数只针对 insert 语句生效，默认为 false；

keyProperty：指定对应的主键属性，也就是mybatis获取到主键后，讲这个值封装给JavaBean的哪个属性。</code></pre>
<h4 id="1-3-1-未配置"><a href="#1-3-1-未配置" class="headerlink" title="1.3.1 未配置"></a>1.3.1 未配置</h4><p>我们先看一个例子。</p>
<pre class=" language-xml"><code class="language-xml"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8" ?></span>
<span class="token doctype">&lt;!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>mapper</span> <span class="token attr-name">namespace</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.dao.UserDao<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectUserById<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.User<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        select * from user where id = #{id}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--&lt;insert id="saveUser" useGeneratedKeys="true" keyProperty="id">--></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>insert</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>saveUser<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        insert into user(user_name) values(#{userName})
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>insert</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>mapper</span><span class="token punctuation">></span></span></code></pre>
<p>测试代码</p>
<pre class=" language-java"><code class="language-java">    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test07</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        UserDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>UserDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        User user <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        user<span class="token punctuation">.</span><span class="token function">setUserName</span><span class="token punctuation">(</span><span class="token string">"lisi"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        Integer count <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">saveUser</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"插入成功条数: "</span><span class="token operator">+</span> count<span class="token punctuation">)</span><span class="token punctuation">;</span>
        sqlSession<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span></code></pre>
<p>运行结果</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: insert into user(user_name) values(?) 
DEBUG [main] - ==> Parameters: lisi(String)
DEBUG [main] - <==    Updates: 1
User{id=null, userName='lisi'}
插入成功条数: 1</code></pre>
<p>但是发现id是null，也就是说id[主键]不能使用，此时我们就的需要配置那个参数了</p>
<h4 id="1-3-2-配置"><a href="#1-3-2-配置" class="headerlink" title="1.3.2 配置"></a>1.3.2 配置</h4><pre class=" language-xml"><code class="language-xml"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8" ?></span>
<span class="token doctype">&lt;!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>mapper</span> <span class="token attr-name">namespace</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.dao.UserDao<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectUserById<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.User<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        select * from user where id = #{id}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>insert</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>saveUser<span class="token punctuation">"</span></span> <span class="token attr-name">useGeneratedKeys</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span> <span class="token attr-name">keyProperty</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>id<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        insert into user(user_name) values(#{userName})
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>insert</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>mapper</span><span class="token punctuation">></span></span></code></pre>
<p>再次运行测试代码,查看输出</p>
<pre><code>DEBUG [main] - ==&gt;  Preparing: insert into user(user_name) values(?) 
DEBUG [main] - ==&gt; Parameters: lisi(String)
DEBUG [main] - &lt;==    Updates: 1
User{id=8, userName=&#39;lisi&#39;}
插入成功条数: 1</code></pre><blockquote>
<p>你看id就能拿到了吧</p>
</blockquote>
<p>当完成插入操作的时候，可以将自增生成的主键值返回到具体的对象。</p>
<p>keyProperty: 指定返回的主键要赋值到哪个属性中。</p>
<blockquote>
<p>在编写sql语句的过程中，无论你是否配置了驼峰标识的识别settings，都需要在sql语句总写具体的属性名，不能写对象的名称</p>
</blockquote>
<p>User.class</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>bean<span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/**
 * @author summer
 * @create 2020-04-08 17:42
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> Integer id<span class="token punctuation">;</span>
    <span class="token keyword">private</span> String userName<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token function">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token function">User</span><span class="token punctuation">(</span>Integer id<span class="token punctuation">,</span> String userName<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>userName <span class="token operator">=</span> userName<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> Integer <span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> id<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setId</span><span class="token punctuation">(</span>Integer id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> String <span class="token function">getUserName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> userName<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setUserName</span><span class="token punctuation">(</span>String userName<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>userName <span class="token operator">=</span> userName<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">"User{"</span> <span class="token operator">+</span>
                <span class="token string">"id="</span> <span class="token operator">+</span> id <span class="token operator">+</span>
                <span class="token string">", userName='"</span> <span class="token operator">+</span> userName <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span>
                <span class="token string">'}'</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<p>UserDao.xml</p>
<pre class=" language-xml"><code class="language-xml"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8" ?></span>
<span class="token doctype">&lt;!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>mapper</span> <span class="token attr-name">namespace</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.dao.UserDao<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectUserById<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.User<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        select * from user where id = #{id}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>insert</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>saveUser<span class="token punctuation">"</span></span> <span class="token attr-name">useGeneratedKeys</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span> <span class="token attr-name">keyProperty</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>id<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        insert into user(user_name) values(#{userName})
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>insert</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>mapper</span><span class="token punctuation">></span></span></code></pre>
<blockquote>
<p> user(user_name) 后面的要写数据中表的字段。     </p>
</blockquote>
<pre class=" language-mysql"><code class="language-mysql">mysql> desc user;
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| id        | int(11)      | NO   | PRI | NULL    | auto_increment |
| user_name | varchar(255) | YES  |     | NULL    |                |
+-----------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)</code></pre>
<h2 id="2-select元素属性"><a href="#2-select元素属性" class="headerlink" title="2. select元素属性"></a>2. select元素属性</h2><p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200415105124.png" alt="image-20200415105122927"></p>
<table>
<thead>
<tr>
<th align="left">属性</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><code>id</code></td>
<td align="left">在命名空间中唯一的标识符，可以被用来引用这条语句。</td>
</tr>
<tr>
<td align="left"><code>parameterType</code></td>
<td align="left">将会传入这条语句的参数的类全限定名或别名。这个属性是可选的，因为 MyBatis 可以通过类型处理器（TypeHandler）推断出具体传入语句的参数，默认值为未设置（unset）。</td>
</tr>
<tr>
<td align="left">parameterMap</td>
<td align="left">用于引用外部 parameterMap 的属性，目前已被废弃。请使用行内参数映射和 parameterType 属性。</td>
</tr>
<tr>
<td align="left"><code>resultType</code></td>
<td align="left">期望从这条语句中返回结果的类全限定名或别名。 注意，如果返回的是集合，那应该设置为集合包含的类型，而不是集合本身的类型。 resultType 和 resultMap 之间只能同时使用一个。</td>
</tr>
<tr>
<td align="left"><code>resultMap</code></td>
<td align="left">对外部 resultMap 的命名引用。结果映射是 MyBatis 最强大的特性，如果你对其理解透彻，许多复杂的映射问题都能迎刃而解。 resultType 和 resultMap 之间只能同时使用一个。</td>
</tr>
<tr>
<td align="left"><code>flushCache</code></td>
<td align="left">将其设置为 true 后，只要语句被调用，都会导致本地缓存和二级缓存被清空，默认值：false。</td>
</tr>
<tr>
<td align="left"><code>useCache</code></td>
<td align="left">将其设置为 true 后，将会导致本条语句的结果被二级缓存缓存起来，默认值：对 select 元素为 true。</td>
</tr>
<tr>
<td align="left"><code>timeout</code></td>
<td align="left">这个设置是在抛出异常之前，驱动程序等待数据库返回请求结果的秒数。默认值为未设置（unset）（依赖数据库驱动）。</td>
</tr>
<tr>
<td align="left"><code>fetchSize</code></td>
<td align="left">这是一个给驱动的建议值，尝试让驱动程序每次批量返回的结果行数等于这个设置值。 默认值为未设置（unset）（依赖驱动）。</td>
</tr>
<tr>
<td align="left"><code>statementType</code></td>
<td align="left">可选 STATEMENT，PREPARED 或 CALLABLE。这会让 MyBatis 分别使用 Statement，PreparedStatement 或 CallableStatement，默认值：PREPARED。</td>
</tr>
<tr>
<td align="left"><code>resultSetType</code></td>
<td align="left">FORWARD_ONLY，SCROLL_SENSITIVE, SCROLL_INSENSITIVE 或 DEFAULT（等价于 unset） 中的一个，默认值为 unset （依赖数据库驱动）。</td>
</tr>
<tr>
<td align="left"><code>databaseId</code></td>
<td align="left">如果配置了数据库厂商标识（databaseIdProvider），MyBatis 会加载所有不带 databaseId 或匹配当前 databaseId 的语句；如果带和不带的语句都有，则不带的会被忽略。</td>
</tr>
<tr>
<td align="left"><code>resultOrdered</code></td>
<td align="left">这个设置仅针对嵌套结果 select 语句：如果为 true，将会假设包含了嵌套结果集或是分组，当返回一个主结果行时，就不会产生对前面结果集的引用。 这就使得在获取嵌套结果集的时候不至于内存不够用。默认值：<code>false</code>。</td>
</tr>
<tr>
<td align="left"><code>resultSets</code></td>
<td align="left">这个设置仅适用于多结果集的情况。它将列出语句执行后返回的结果集并赋予每个结果集一个名称，多个名称之间以逗号分隔。</td>
</tr>
</tbody></table>
<p>&emsp;&emsp;查询语句是 MyBatis 中最常用的元素之一——光能把数据存到数据库中价值并不大，还要能重新取出来才有用，多数应用也都是查询比修改要频繁。 MyBatis 的基本原则之一是：在每个插入、更新或删除操作之间，通常会执行多个查询操作。因此，MyBatis 在查询和结果映射做了相当多的改进。</p>
<blockquote>
<p><code>resultType</code>和<code>resultMap</code>用的非常的多。需要重点掌握。</p>
</blockquote>
<h3 id="2-1-resultType-resultMap"><a href="#2-1-resultType-resultMap" class="headerlink" title="2.1 resultType/resultMap"></a>2.1 resultType/resultMap</h3><ul>
<li>resultType:表示返回的结果的类型,一般使用的并不多，此类型只能返回单一的对象，而我们在查询的时候特别是关联查询的时候，需要自定义结果集合，当返回的结果是一个集合的时候，并不需要resultmap，只需要使用resulttype指定集合中的元素类型即可</li>
<li>resultMap:当进行关联查询的时候，在返回结果的对象中还包含另一个对象的引用时，此时需要自定义结果集合，使用resultmap</li>
<li>二者只能选取其中一个</li>
</ul>
<h4 id="2-1-1-resultType"><a href="#2-1-1-resultType" class="headerlink" title="2.1.1 resultType"></a>2.1.1 resultType</h4><p>新加UserDao.class中加入如下</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> List<span class="token operator">&lt;</span>Emp<span class="token operator">></span> <span class="token function">selectAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>dao<span class="token punctuation">;</span>

<span class="token keyword">import</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>bean<span class="token punctuation">.</span>Emp<span class="token punctuation">;</span>

<span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>List<span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/**
 * @author summer
 * @create 2020-04-08 16:35
 */</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">EmpDao</span> <span class="token punctuation">{</span>

    <span class="token keyword">public</span> Integer <span class="token function">save</span><span class="token punctuation">(</span>Emp emp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> Integer <span class="token function">update</span><span class="token punctuation">(</span>Emp emp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> Integer <span class="token function">delete</span><span class="token punctuation">(</span>Integer empno<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> Emp <span class="token function">selectEmpByEmpno</span><span class="token punctuation">(</span>Integer empno<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> List<span class="token operator">&lt;</span>Emp<span class="token operator">></span> <span class="token function">selectAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>java
<span class="token punctuation">}</span></code></pre>
<p>xml修改</p>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectAll<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
  select * from emp
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<p>测试</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test08</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    EmpDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>EmpDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    List<span class="token operator">&lt;</span>Emp<span class="token operator">></span> list <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>Emp emp <span class="token operator">:</span> list<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>emp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>运行结果</p>
<pre class=" language-html"><code class="language-html">DEBUG [main] - ==>  Preparing: select * from emp 
DEBUG [main] - ==> Parameters: 
TRACE [main] - &lt;==    Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
TRACE [main] - &lt;==        Row: 3333, 张三, null, null, null, null, null, null
TRACE [main] - &lt;==        Row: 7369, SMITH, CLERK, 7902.0, 1980-12-17, 800.0, null, 20
TRACE [main] - &lt;==        Row: 7499, ALLEN, SALESMAN, 7698.0, 1981-02-20, 1600.0, 300.0, 30
TRACE [main] - &lt;==        Row: 7521, WARD, SALESMAN, 7698.0, 1981-02-22, 1250.0, 500.0, 30
TRACE [main] - &lt;==        Row: 7566, JONES, MANAGER, 7839.0, 1981-04-02, 2975.0, null, 20
TRACE [main] - &lt;==        Row: 7654, MARTIN, SALESMAN, 7698.0, 1981-09-28, 1250.0, 1400.0, 30
TRACE [main] - &lt;==        Row: 7698, BLAKE, MANAGER, 7839.0, 1981-05-01, 2850.0, null, 30
TRACE [main] - &lt;==        Row: 7782, CLARK, MANAGER, 7839.0, 1981-06-09, 2450.0, null, 10
TRACE [main] - &lt;==        Row: 7788, SCOTT, ANALYST, 7566.0, 1987-07-13, 3000.0, null, 20
TRACE [main] - &lt;==        Row: 7839, KING, PRESIDENT, null, 1981-11-17, 5000.0, null, 10
TRACE [main] - &lt;==        Row: 7844, TURNER, SALESMAN, 7698.0, 1981-09-08, 1500.0, 0.0, 30
TRACE [main] - &lt;==        Row: 7876, ADAMS, CLERK, 7788.0, 1987-07-13, 1100.0, null, 20
TRACE [main] - &lt;==        Row: 7900, JAMES, CLERK, 7698.0, 1981-12-03, 950.0, null, 30
TRACE [main] - &lt;==        Row: 7902, FORD, ANALYST, 7566.0, 1981-12-03, 3000.0, null, 20
TRACE [main] - &lt;==        Row: 7934, MILLER, CLERK, 7782.0, 1982-01-23, 1300.0, null, 10
DEBUG [main] - &lt;==      Total: 15
Emp{empno=3333, ename='张三', job='null', mgr=null, hiredate=null, sal=null, common=null, deptno=null}
Emp{empno=7369, ename='SMITH', job='CLERK', mgr=7902, hiredate=Wed Dec 17 08:00:00 CST 1980, sal=800.0, common=null, deptno=20}
Emp{empno=7499, ename='ALLEN', job='SALESMAN', mgr=7698, hiredate=Fri Feb 20 08:00:00 CST 1981, sal=1600.0, common=null, deptno=30}
Emp{empno=7521, ename='WARD', job='SALESMAN', mgr=7698, hiredate=Sun Feb 22 08:00:00 CST 1981, sal=1250.0, common=null, deptno=30}
Emp{empno=7566, ename='JONES', job='MANAGER', mgr=7839, hiredate=Thu Apr 02 08:00:00 CST 1981, sal=2975.0, common=null, deptno=20}
Emp{empno=7654, ename='MARTIN', job='SALESMAN', mgr=7698, hiredate=Mon Sep 28 08:00:00 CST 1981, sal=1250.0, common=null, deptno=30}
Emp{empno=7698, ename='BLAKE', job='MANAGER', mgr=7839, hiredate=Fri May 01 08:00:00 CST 1981, sal=2850.0, common=null, deptno=30}
Emp{empno=7782, ename='CLARK', job='MANAGER', mgr=7839, hiredate=Tue Jun 09 08:00:00 CST 1981, sal=2450.0, common=null, deptno=10}
Emp{empno=7788, ename='SCOTT', job='ANALYST', mgr=7566, hiredate=Mon Jul 13 09:00:00 CDT 1987, sal=3000.0, common=null, deptno=20}
Emp{empno=7839, ename='KING', job='PRESIDENT', mgr=null, hiredate=Tue Nov 17 08:00:00 CST 1981, sal=5000.0, common=null, deptno=10}
Emp{empno=7844, ename='TURNER', job='SALESMAN', mgr=7698, hiredate=Tue Sep 08 08:00:00 CST 1981, sal=1500.0, common=null, deptno=30}
Emp{empno=7876, ename='ADAMS', job='CLERK', mgr=7788, hiredate=Mon Jul 13 09:00:00 CDT 1987, sal=1100.0, common=null, deptno=20}
Emp{empno=7900, ename='JAMES', job='CLERK', mgr=7698, hiredate=Thu Dec 03 08:00:00 CST 1981, sal=950.0, common=null, deptno=30}
Emp{empno=7902, ename='FORD', job='ANALYST', mgr=7566, hiredate=Thu Dec 03 08:00:00 CST 1981, sal=3000.0, common=null, deptno=20}
Emp{empno=7934, ename='MILLER', job='CLERK', mgr=7782, hiredate=Sat Jan 23 08:00:00 CST 1982, sal=1300.0, common=null, deptno=10}</code></pre>
<blockquote>
<p>可以看到查出了很多数据，放到了list中了，然后打印。</p>
</blockquote>
<h3 id="2-2-结果返回list集合"><a href="#2-2-结果返回list集合" class="headerlink" title="2.2 结果返回list集合"></a>2.2 结果返回list集合</h3><p>&emsp;&emsp;有时候我们查询的数据需要返回list。</p>
<p><code>EmpDao.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> List<span class="token operator">&lt;</span>Emp<span class="token operator">></span> <span class="token function">selectEmpByEnameLike</span><span class="token punctuation">(</span>String ename<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p><code>EmpDao.xml</code></p>
<pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!--resultType 如果返回的是一个集合，要写集合中的元素的类型--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByEnameLike<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    select * from emp where ename like #{ename}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<blockquote>
<ol>
<li>resultType 如果返回的是一个集合，要写集合中的元素的类型</li>
<li>MyBatis会吧返回的类型转换resultType配置的类型装入List集合</li>
</ol>
</blockquote>
<p><code>MyTest.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/*通过名字模糊查询返回List*/</span>
<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test14</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    EmpDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>EmpDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    List<span class="token operator">&lt;</span>Emp<span class="token operator">></span> emps <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectEmpByEnameLike</span><span class="token punctuation">(</span><span class="token string">"%M%"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>Emp emp1 <span class="token operator">:</span> emps<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>emp1<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>代码是查询名字中带有M的员工信息。运行后的结果</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: select * from emp where ename like ? 
DEBUG [main] - ==> Parameters: %M%(String)
TRACE [main] - <==    Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
TRACE [main] - <==        Row: 7369, SMITH, CLERK, 7902.0, 1980-12-17, 800.0, null, 20
TRACE [main] - <==        Row: 7654, MARTIN, SALESMAN, 7698.0, 1981-09-28, 1250.0, 1400.0, 30
TRACE [main] - <==        Row: 7876, ADAMS, CLERK, 7788.0, 1987-07-13, 1100.0, null, 20
TRACE [main] - <==        Row: 7900, JAMES, CLERK, 7698.0, 1981-12-03, 950.0, null, 30
TRACE [main] - <==        Row: 7934, MILLER, CLERK, 7782.0, 1982-01-23, 1300.0, null, 10
DEBUG [main] - <==      Total: 5
Emp{empno=7369, ename='SMITH', job='CLERK', mgr=7902, hiredate=Wed Dec 17 08:00:00 CST 1980, sal=800.0, common=null, deptno=20}
Emp{empno=7654, ename='MARTIN', job='SALESMAN', mgr=7698, hiredate=Mon Sep 28 08:00:00 CST 1981, sal=1250.0, common=null, deptno=30}
Emp{empno=7876, ename='ADAMS', job='CLERK', mgr=7788, hiredate=Mon Jul 13 09:00:00 CDT 1987, sal=1100.0, common=null, deptno=20}
Emp{empno=7900, ename='JAMES', job='CLERK', mgr=7698, hiredate=Thu Dec 03 08:00:00 CST 1981, sal=950.0, common=null, deptno=30}
Emp{empno=7934, ename='MILLER', job='CLERK', mgr=7782, hiredate=Sat Jan 23 08:00:00 CST 1982, sal=1300.0, common=null, deptno=10}
</code></pre>
<p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200415095906.png" alt="结果返回List集合"></p>
<h3 id="2-3-结果返回Map集合"><a href="#2-3-结果返回Map集合" class="headerlink" title="2.3 结果返回Map集合"></a>2.3 结果返回Map集合</h3><h4 id="2-3-1-返回单条数据"><a href="#2-3-1-返回单条数据" class="headerlink" title="2.3.1 返回单条数据"></a>2.3.1 返回单条数据</h4><blockquote>
<pre><code>返回一条记录的map，key就是列名字，值就是对应的值,类似
{ENAME=SMITH, HIREDATE=1980-12-17, EMPNO=7369, MGR=7902.0, JOB=CLERK, DEPTNO=20, SAL=800.0}</code></pre></blockquote>
<p><code>EmpDao.java</code></p>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/*返回一条记录的map，key就是列名字，值就是对应的值*/</span>
<span class="token keyword">public</span> Map<span class="token operator">&lt;</span>Object<span class="token punctuation">,</span>Object<span class="token operator">></span> <span class="token function">selectEmpByEmpnoReturnMap</span><span class="token punctuation">(</span>Integer empno<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p><code>EmpDao.xml</code></p>
<pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!--当返回值是map结构的时候，会把查询结构的字段作为key，结果作为value--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByEmpnoReturnMap<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>map<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
  select * from emp where empno=#{empno}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<p><code>MyTest.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test12</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    EmpDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>EmpDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    Map<span class="token operator">&lt;</span>Object<span class="token punctuation">,</span> Object<span class="token operator">></span> objectObjectMap <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectEmpByEmpnoReturnMap</span><span class="token punctuation">(</span><span class="token number">7369</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>objectObjectMap<span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"-----循环输出------"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span>Map<span class="token punctuation">.</span>Entry<span class="token operator">&lt;</span>Object<span class="token punctuation">,</span>Object<span class="token operator">></span> entry<span class="token operator">:</span>objectObjectMap<span class="token punctuation">.</span><span class="token function">entrySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        Object mapkey <span class="token operator">=</span> entry<span class="token punctuation">.</span><span class="token function">getKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        Object mapvalue <span class="token operator">=</span> entry<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>mapkey<span class="token operator">+</span><span class="token string">":"</span><span class="token operator">+</span>mapvalue<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>结果输出</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: select * from emp where empno=? 
DEBUG [main] - ==> Parameters: 7369(Integer)
TRACE [main] - <==    Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
TRACE [main] - <==        Row: 7369, SMITH, CLERK, 7902.0, 1980-12-17, 800.0, null, 20
DEBUG [main] - <==      Total: 1
{ENAME=SMITH, HIREDATE=1980-12-17, EMPNO=7369, MGR=7902.0, JOB=CLERK, DEPTNO=20, SAL=800.0}
-----循环输出------
ENAME:SMITH
HIREDATE:1980-12-17
EMPNO:7369
MGR:7902.0
JOB:CLERK
DEPTNO:20
SAL:800.0

Process finished with exit code 0</code></pre>
<h4 id="2-3-2-返回多条数据"><a href="#2-3-2-返回多条数据" class="headerlink" title="2.3.2 返回多条数据"></a>2.3.2 返回多条数据</h4><p>&emsp;&emsp;当返回的结果是一个map的集合时候，同时map中包含多个对象，那么此时必须要在dao方法上添加MapKey注解，来标识哪一个属性值作为key</p>
<p><code>EmpDao.java</code></p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@MapKey</span><span class="token punctuation">(</span><span class="token string">"empno"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> Map<span class="token operator">&lt;</span>String<span class="token punctuation">,</span>Emp<span class="token operator">></span> <span class="token function">selectAll2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<blockquote>
<p>必须通过注解@MapKey(“empno”)来表明哪个是key。empno是表示map的key，否则报错</p>
<p>org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 15</p>
</blockquote>
<p><code>EmpDao.xml</code></p>
<pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!--当返回的结果是一个map的集合时候，同时map中包含多个对象，那么此时必须要在dao方法上添加MapKey注解，来标识哪一个属性值作为key--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectAll2<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
  select * from emp
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<p><code>MyTest.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/*自定义map,返回多个数据，模糊查询*/</span>
<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test15</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    EmpDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>EmpDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    Map<span class="token operator">&lt;</span>String<span class="token punctuation">,</span> Emp<span class="token operator">></span> stringEmpMap <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectEmpByEnameLikeReturnMap</span><span class="token punctuation">(</span><span class="token string">"%M%"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stringEmpMap<span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"-------------"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    Iterator<span class="token operator">&lt;</span>Map<span class="token punctuation">.</span>Entry<span class="token operator">&lt;</span>String<span class="token punctuation">,</span> Emp<span class="token operator">>></span> entries <span class="token operator">=</span> stringEmpMap<span class="token punctuation">.</span><span class="token function">entrySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>entries<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        Map<span class="token punctuation">.</span>Entry<span class="token operator">&lt;</span>String<span class="token punctuation">,</span> Emp<span class="token operator">></span> entry <span class="token operator">=</span> entries<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        String key <span class="token operator">=</span> entry<span class="token punctuation">.</span><span class="token function">getKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        Emp value <span class="token operator">=</span> entry<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>key<span class="token operator">+</span><span class="token string">":"</span><span class="token operator">+</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>输出结果</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: select * from emp where ename like ? 
DEBUG [main] - ==> Parameters: %M%(String)
TRACE [main] - <==    Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
TRACE [main] - <==        Row: 7369, SMITH, CLERK, 7902.0, 1980-12-17, 800.0, null, 20
TRACE [main] - <==        Row: 7654, MARTIN, SALESMAN, 7698.0, 1981-09-28, 1250.0, 1400.0, 30
TRACE [main] - <==        Row: 7876, ADAMS, CLERK, 7788.0, 1987-07-13, 1100.0, null, 20
TRACE [main] - <==        Row: 7900, JAMES, CLERK, 7698.0, 1981-12-03, 950.0, null, 30
TRACE [main] - <==        Row: 7934, MILLER, CLERK, 7782.0, 1982-01-23, 1300.0, null, 10
DEBUG [main] - <==      Total: 5
{MARTIN=Emp{empno=7654, ename='MARTIN', job='SALESMAN', mgr=7698, hiredate=Mon Sep 28 08:00:00 CST 1981, sal=1250.0, common=null, deptno=30}, MILLER=Emp{empno=7934, ename='MILLER', job='CLERK', mgr=7782, hiredate=Sat Jan 23 08:00:00 CST 1982, sal=1300.0, common=null, deptno=10}, JAMES=Emp{empno=7900, ename='JAMES', job='CLERK', mgr=7698, hiredate=Thu Dec 03 08:00:00 CST 1981, sal=950.0, common=null, deptno=30}, SMITH=Emp{empno=7369, ename='SMITH', job='CLERK', mgr=7902, hiredate=Wed Dec 17 08:00:00 CST 1980, sal=800.0, common=null, deptno=20}, ADAMS=Emp{empno=7876, ename='ADAMS', job='CLERK', mgr=7788, hiredate=Mon Jul 13 09:00:00 CDT 1987, sal=1100.0, common=null, deptno=20}}
-------------
MARTIN:Emp{empno=7654, ename='MARTIN', job='SALESMAN', mgr=7698, hiredate=Mon Sep 28 08:00:00 CST 1981, sal=1250.0, common=null, deptno=30}
MILLER:Emp{empno=7934, ename='MILLER', job='CLERK', mgr=7782, hiredate=Sat Jan 23 08:00:00 CST 1982, sal=1300.0, common=null, deptno=10}
JAMES:Emp{empno=7900, ename='JAMES', job='CLERK', mgr=7698, hiredate=Thu Dec 03 08:00:00 CST 1981, sal=950.0, common=null, deptno=30}
SMITH:Emp{empno=7369, ename='SMITH', job='CLERK', mgr=7902, hiredate=Wed Dec 17 08:00:00 CST 1980, sal=800.0, common=null, deptno=20}
ADAMS:Emp{empno=7876, ename='ADAMS', job='CLERK', mgr=7788, hiredate=Mon Jul 13 09:00:00 CDT 1987, sal=1100.0, common=null, deptno=20}
</code></pre>
<h3 id="2-4-ResultMap"><a href="#2-4-ResultMap" class="headerlink" title="2.4 ResultMap"></a>2.4 ResultMap</h3><h4 id="2-4-1-映射级联属性"><a href="#2-4-1-映射级联属性" class="headerlink" title="2.4.1 映射级联属性"></a>2.4.1 映射级联属性</h4><ul>
<li>全局的settings设置<ul>
<li>autoMappingBehavior默认是PARTIAL，开启自动映射的功能，唯一的要求就是列名和javaBean属性名一致。</li>
<li>如果autoMappingBehavior设置为null会取消自动映射</li>
<li>数据库字段命名规范，POJO属性符合驼峰命名法，如A_COLUMN-&gt;aColumn，我们可以开启自动驼峰命名规范则映射功能，mapUnderscoreToCamelCase=true</li>
</ul>
</li>
<li>自定义的resultMap，实现高级结果集映射。</li>
</ul>
<p>&emsp;&emsp;我们数据库字段和类属性不一致的时候，我们可以有两种方式</p>
<pre class=" language-sql"><code class="language-sql">mysql<span class="token operator">></span> <span class="token keyword">desc</span> dog<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment" spellcheck="true">---------+--------------+------+-----+---------+----------------+</span>
<span class="token operator">|</span> Field   <span class="token operator">|</span> <span class="token keyword">Type</span>         <span class="token operator">|</span> <span class="token boolean">Null</span> <span class="token operator">|</span> <span class="token keyword">Key</span> <span class="token operator">|</span> <span class="token keyword">Default</span> <span class="token operator">|</span> Extra          <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment" spellcheck="true">---------+--------------+------+-----+---------+----------------+</span>
<span class="token operator">|</span> id      <span class="token operator">|</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span>      <span class="token operator">|</span> <span class="token keyword">NO</span>   <span class="token operator">|</span> PRI <span class="token operator">|</span> <span class="token boolean">NULL</span>    <span class="token operator">|</span> <span class="token keyword">auto_increment</span> <span class="token operator">|</span>
<span class="token operator">|</span> dname   <span class="token operator">|</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token operator">|</span> YES  <span class="token operator">|</span>     <span class="token operator">|</span> <span class="token boolean">NULL</span>    <span class="token operator">|</span>                <span class="token operator">|</span>
<span class="token operator">|</span> dage    <span class="token operator">|</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span>     <span class="token operator">|</span> YES  <span class="token operator">|</span>     <span class="token operator">|</span> <span class="token boolean">NULL</span>    <span class="token operator">|</span>                <span class="token operator">|</span>
<span class="token operator">|</span> dgender <span class="token operator">|</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token operator">|</span> YES  <span class="token operator">|</span>     <span class="token operator">|</span> <span class="token boolean">NULL</span>    <span class="token operator">|</span>                <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment" spellcheck="true">---------+--------------+------+-----+---------+----------------+</span>
<span class="token number">4</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>

mysql<span class="token operator">></span> <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> dog<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment" spellcheck="true">----+-------+------+---------+</span>
<span class="token operator">|</span> id <span class="token operator">|</span> dname <span class="token operator">|</span> dage <span class="token operator">|</span> dgender <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment" spellcheck="true">----+-------+------+---------+</span>
<span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span> 大黄  <span class="token operator">|</span>    <span class="token number">1</span> <span class="token operator">|</span> 公      <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span> 二黄  <span class="token operator">|</span>    <span class="token number">2</span> <span class="token operator">|</span> 母      <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span> 三黄  <span class="token operator">|</span>    <span class="token number">3</span> <span class="token operator">|</span> 公      <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment" spellcheck="true">----+-------+------+---------+</span>
<span class="token number">3</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.01</span> sec<span class="token punctuation">)</span></code></pre>
<p><code>Dog.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>bean<span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Dog</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> Integer id<span class="token punctuation">;</span>
    <span class="token keyword">private</span> String name<span class="token punctuation">;</span>
    <span class="token keyword">private</span> Integer age<span class="token punctuation">;</span>
    <span class="token keyword">private</span> String gender<span class="token punctuation">;</span>

    <span class="token keyword">public</span> Integer <span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> id<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setId</span><span class="token punctuation">(</span>Integer id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> String <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span>String name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> Integer <span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> age<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setAge</span><span class="token punctuation">(</span>Integer age<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> String <span class="token function">getGender</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> gender<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setGender</span><span class="token punctuation">(</span>String gender<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>gender <span class="token operator">=</span> gender<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">"Dog{"</span> <span class="token operator">+</span>
                <span class="token string">"id="</span> <span class="token operator">+</span> id <span class="token operator">+</span>
                <span class="token string">", name='"</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span>
                <span class="token string">", age="</span> <span class="token operator">+</span> age <span class="token operator">+</span>
                <span class="token string">", gender='"</span> <span class="token operator">+</span> gender <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span>
                <span class="token string">'}'</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<p>对比</p>
<table>
<thead>
<tr>
<th>mysql字段</th>
<th>java类[Dog.class]</th>
</tr>
</thead>
<tbody><tr>
<td>id</td>
<td>id</td>
</tr>
<tr>
<td>dname</td>
<td>name</td>
</tr>
<tr>
<td>dage</td>
<td>age</td>
</tr>
<tr>
<td>dgender</td>
<td>gender</td>
</tr>
</tbody></table>
<p>两者不对应，sql会对应不上。解决该问题，有以下两个方法。</p>
<ul>
<li>sql的时候alias</li>
<li>或者直接使用resultmap设置他们之间的关系</li>
</ul>
<p><code>DogDao.java</code></p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>dao<span class="token punctuation">;</span>
<span class="token keyword">import</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>bean<span class="token punctuation">.</span>Dog<span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">DogDao</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> Dog <span class="token function">selectDogById</span><span class="token punctuation">(</span>Integer id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> Dog <span class="token function">selectDogById1</span><span class="token punctuation">(</span>Integer id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> Dog <span class="token function">selectDogById2</span><span class="token punctuation">(</span>Integer id<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p><code>DogDao.xml</code></p>
<pre class=" language-xml"><code class="language-xml"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8" ?></span>
<span class="token doctype">&lt;!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"></span>
<span class="token comment" spellcheck="true">&lt;!--在使用mybatis的时候，有些情况下需要我们封装结果集，一般情况下mybatis会帮我们自动封装（字段名跟属性值必须一一对应），
    但是如果字段的值和类中的值,不匹配的时候，怎么处理？
    1、可以在sql语句中添加别名字段，来保证赋值成功，但是太麻烦了，而且不可重用
    2、resultMap
--></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>mapper</span> <span class="token attr-name">namespace</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.dao.DogDao<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--自定义结果集
    id:表示当前结果集的唯一标识
    type:表示当前结果集的类型
    --></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>resultMap</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>myDog<span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Dog<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        <span class="token comment" spellcheck="true">&lt;!--
        id:表示指定对应的主键,底层会优化，主键也可以使用result
        property:实体类中对应的属性值
        column：表中字段的名称
        --></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>id<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>id<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token comment" spellcheck="true">&lt;!--除主键外的其他字段映射--></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>name<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dname<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>age<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dage<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>gender<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dgender<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>resultMap</span><span class="token punctuation">></span></span>

    <span class="token comment" spellcheck="true">&lt;!--resultMap 自定义结果集映射规则--></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectDogById<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>myDog<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        select * from dog where id = #{id}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>

    <span class="token comment" spellcheck="true">&lt;!--直接是不显示的--></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectDogById1<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Dog<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        select * from dog where id = #{id}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>

    <span class="token comment" spellcheck="true">&lt;!--通过别名显示--></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectDogById2<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Dog<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        select id id,dname name,dage age,dgender gender from dog where id = #{id}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>mapper</span><span class="token punctuation">></span></span></code></pre>
<p><code>MyTest.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">import</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>bean<span class="token punctuation">.</span>Dog<span class="token punctuation">;</span>
<span class="token keyword">import</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>dao<span class="token punctuation">.</span>DogDao<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>io<span class="token punctuation">.</span>Resources<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>session<span class="token punctuation">.</span>SqlSession<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>session<span class="token punctuation">.</span>SqlSessionFactory<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>session<span class="token punctuation">.</span>SqlSessionFactoryBuilder<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>Before<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>Test<span class="token punctuation">;</span>
<span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>IOException<span class="token punctuation">;</span>
<span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>InputStream<span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyTest2</span> <span class="token punctuation">{</span>
    SqlSessionFactory sqlSessionFactory <span class="token operator">=</span>  null<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Before</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        String resource <span class="token operator">=</span> <span class="token string">"mybatis_config.xml"</span><span class="token punctuation">;</span>
        InputStream inputStream <span class="token operator">=</span> null<span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            inputStream <span class="token operator">=</span> Resources<span class="token punctuation">.</span><span class="token function">getResourceAsStream</span><span class="token punctuation">(</span>resource<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        sqlSessionFactory <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SqlSessionFactoryBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span>inputStream<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span>
        <span class="token comment" spellcheck="true">//获取与数据库相关的会话</span>
        SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//获取对应的映射接口对象</span>
        DogDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>DogDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//执行具体的sql语句</span>
        Dog dog <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectDogById</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>dog<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//关闭会话</span>
        sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test01</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span>

        <span class="token comment" spellcheck="true">//获取与数据库相关的会话</span>
        SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//获取对应的映射接口对象</span>
        DogDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>DogDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//执行具体的sql语句</span>
        Dog dog <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectDogById1</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>dog<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//关闭会话</span>
        sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test02</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span>

        <span class="token comment" spellcheck="true">//获取与数据库相关的会话</span>
        SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//获取对应的映射接口对象</span>
        DogDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>DogDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//执行具体的sql语句</span>
        Dog dog <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectDogById2</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>dog<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//关闭会话</span>
        sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<ul>
<li><p>直接执行，不显示结果</p>
<pre class=" language-java"><code class="language-java">Dog dog <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectDogById1</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>输出</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: select * from dog where id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
TRACE [main] - <==    Columns: id, dname, dage, dgender
TRACE [main] - <==        Row: 1, 大黄, 1, 公
DEBUG [main] - <==      Total: 1
Dog{id=1, name='null', age=null, gender='null'}</code></pre>
</li>
<li><p>通过别名，来显示结果</p>
<pre class=" language-java"><code class="language-java">Dog dog <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectDogById2</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>输出</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: select id id,dname name,dage age,dgender gender from dog where id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
TRACE [main] - <==    Columns: id, name, age, gender
TRACE [main] - <==        Row: 1, 大黄, 1, 公
DEBUG [main] - <==      Total: 1
Dog{id=1, name='大黄', age=1, gender='公'}</code></pre>
</li>
<li><p>通过resultMap自定义</p>
<pre class=" language-java"><code class="language-java">Dog dog <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectDogById</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>输出</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: select * from dog where id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
TRACE [main] - <==    Columns: id, dname, dage, dgender
TRACE [main] - <==        Row: 1, 大黄, 1, 公
DEBUG [main] - <==      Total: 1
Dog{id=1, name='大黄', age=1, gender='公'}</code></pre>
<p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200415132050.png" alt="自定义resultMap"></p>
<p>此时就是自己定制了规则，和驼峰命名啥的也没有啥子鸟关系了。自己定义了类属性和数据库中的字段的对应关系。</p>
</li>
</ul>
<h4 id="2-4-2-映射关联查询"><a href="#2-4-2-映射关联查询" class="headerlink" title="2.4.2 映射关联查询"></a>2.4.2 映射关联查询</h4><p><code>Dept.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>bean<span class="token punctuation">;</span>

<span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>Serializable<span class="token punctuation">;</span>
<span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>List<span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Dept</span> <span class="token keyword">implements</span> <span class="token class-name">Serializable</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> Integer deptno<span class="token punctuation">;</span>
    <span class="token keyword">private</span> String dname<span class="token punctuation">;</span>
    <span class="token keyword">private</span> String loc<span class="token punctuation">;</span>

    <span class="token keyword">private</span> List<span class="token operator">&lt;</span>Emp<span class="token operator">></span> emps<span class="token punctuation">;</span>

    <span class="token keyword">public</span> Integer <span class="token function">getDeptno</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> deptno<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setDeptno</span><span class="token punctuation">(</span>Integer deptno<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>deptno <span class="token operator">=</span> deptno<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> String <span class="token function">getDname</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> dname<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setDname</span><span class="token punctuation">(</span>String dname<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>dname <span class="token operator">=</span> dname<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> String <span class="token function">getLoc</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> loc<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setLoc</span><span class="token punctuation">(</span>String loc<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>loc <span class="token operator">=</span> loc<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>


    <span class="token keyword">public</span> List<span class="token operator">&lt;</span>Emp<span class="token operator">></span> <span class="token function">getEmps</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> emps<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setEmps</span><span class="token punctuation">(</span>List<span class="token operator">&lt;</span>Emp<span class="token operator">></span> emps<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>emps <span class="token operator">=</span> emps<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">"Dept{"</span> <span class="token operator">+</span>
                <span class="token string">"deptno="</span> <span class="token operator">+</span> deptno <span class="token operator">+</span>
                <span class="token string">", dname='"</span> <span class="token operator">+</span> dname <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span>
                <span class="token string">", loc='"</span> <span class="token operator">+</span> loc <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span>
                <span class="token string">", emps="</span> <span class="token operator">+</span> emps <span class="token operator">+</span>
                <span class="token string">'}'</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<p><code>Emp.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>bean<span class="token punctuation">;</span>

<span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Date<span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/**
 * @author summer
 * @create 2020-04-08 16:33
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Emp</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> Integer empno<span class="token punctuation">;</span>
    <span class="token keyword">private</span> String ename<span class="token punctuation">;</span>
    <span class="token keyword">private</span> String job<span class="token punctuation">;</span>
    <span class="token keyword">private</span> Integer mgr<span class="token punctuation">;</span>
    <span class="token keyword">private</span> Date hiredate<span class="token punctuation">;</span>
    <span class="token keyword">private</span> Double sal<span class="token punctuation">;</span>
    <span class="token keyword">private</span> Double common<span class="token punctuation">;</span>
    <span class="token keyword">private</span> Dept dept<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token function">Emp</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token function">Emp</span><span class="token punctuation">(</span>Integer empno<span class="token punctuation">,</span> String ename<span class="token punctuation">,</span> String job<span class="token punctuation">,</span> Integer mgr<span class="token punctuation">,</span> Date hiredate<span class="token punctuation">,</span> Double sal<span class="token punctuation">,</span> Double common<span class="token punctuation">,</span> Dept dept<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>empno <span class="token operator">=</span> empno<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>ename <span class="token operator">=</span> ename<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>job <span class="token operator">=</span> job<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>mgr <span class="token operator">=</span> mgr<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>hiredate <span class="token operator">=</span> hiredate<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>sal <span class="token operator">=</span> sal<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>common <span class="token operator">=</span> common<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>dept <span class="token operator">=</span> dept<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> Integer <span class="token function">getEmpno</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> empno<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setEmpno</span><span class="token punctuation">(</span>Integer empno<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>empno <span class="token operator">=</span> empno<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> String <span class="token function">getEname</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> ename<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setEname</span><span class="token punctuation">(</span>String ename<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>ename <span class="token operator">=</span> ename<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> String <span class="token function">getJob</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> job<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setJob</span><span class="token punctuation">(</span>String job<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>job <span class="token operator">=</span> job<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> Integer <span class="token function">getMgr</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> mgr<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setMgr</span><span class="token punctuation">(</span>Integer mgr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>mgr <span class="token operator">=</span> mgr<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> Date <span class="token function">getHiredate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> hiredate<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setHiredate</span><span class="token punctuation">(</span>Date hiredate<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>hiredate <span class="token operator">=</span> hiredate<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> Double <span class="token function">getSal</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> sal<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setSal</span><span class="token punctuation">(</span>Double sal<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>sal <span class="token operator">=</span> sal<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> Double <span class="token function">getCommon</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> common<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setCommon</span><span class="token punctuation">(</span>Double common<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>common <span class="token operator">=</span> common<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> Dept <span class="token function">getDept</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> dept<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setDept</span><span class="token punctuation">(</span>Dept dept<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>dept <span class="token operator">=</span> dept<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">"Emp{"</span> <span class="token operator">+</span>
                <span class="token string">"empno="</span> <span class="token operator">+</span> empno <span class="token operator">+</span>
                <span class="token string">", ename='"</span> <span class="token operator">+</span> ename <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span>
                <span class="token string">", job='"</span> <span class="token operator">+</span> job <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span>
                <span class="token string">", mgr="</span> <span class="token operator">+</span> mgr <span class="token operator">+</span>
                <span class="token string">", hiredate="</span> <span class="token operator">+</span> hiredate <span class="token operator">+</span>
                <span class="token string">", sal="</span> <span class="token operator">+</span> sal <span class="token operator">+</span>
                <span class="token string">", common="</span> <span class="token operator">+</span> common <span class="token operator">+</span>
                <span class="token string">", dept="</span> <span class="token operator">+</span> dept <span class="token operator">+</span>
                <span class="token string">'}'</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<p><code>DeptDao.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>dao<span class="token punctuation">;</span>
<span class="token keyword">import</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>bean<span class="token punctuation">.</span>Dept<span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">DeptDao</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> Dept <span class="token function">selectDeptByDeptno</span><span class="token punctuation">(</span>Integer deptno<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> Dept <span class="token function">selectDeptByStep</span><span class="token punctuation">(</span>Integer deptno<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> Dept <span class="token function">selectDeptByStemp2</span><span class="token punctuation">(</span>Integer deptno<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p><code>EmpDao.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>dao<span class="token punctuation">;</span>
<span class="token keyword">import</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>bean<span class="token punctuation">.</span>Emp<span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">EmpDao</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> Emp <span class="token function">seletEmpByEmpno</span><span class="token punctuation">(</span>Integer empno<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> Emp <span class="token function">selectEmpByStep</span><span class="token punctuation">(</span>Integer empno<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p><code>DeptDao.xml</code></p>
<pre class=" language-xml"><code class="language-xml"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8" ?></span>
<span class="token doctype">&lt;!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>mapper</span> <span class="token attr-name">namespace</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.dao.DeptDao<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>resultMap</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>myDept<span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Dept<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dname<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dname<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>loc<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>loc<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>collection</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>emps<span class="token punctuation">"</span></span> <span class="token attr-name">ofType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ename<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ename<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>job<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>job<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>mgr<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>mgr<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>hiredate<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>hiredate<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sal<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sal<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>comm<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>comm<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>collection</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>resultMap</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectDeptByDeptno<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>myDept<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
       select * from dept left join emp on dept.deptno = emp.deptno where dept.deptno = #{deptno}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectDeptByStep<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Dept<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        select * from dept where deptno = #{deptno}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectDeptByStemp2<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptEmp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        select * from dept where deptno = #{deptno}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>resultMap</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptEmp<span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Dept<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dname<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dname<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>loc<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>loc<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>collection</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>emps<span class="token punctuation">"</span></span> <span class="token attr-name">ofType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span> <span class="token attr-name">select</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.dao.EmpDao.selectEmpByStep2<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span> <span class="token attr-name">fetchType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>lazy<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>

        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>collection</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>resultMap</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>mapper</span><span class="token punctuation">></span></span></code></pre>
<p><code>EmpDao.xml</code></p>
<pre class=" language-xml"><code class="language-xml"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8" ?></span>
<span class="token doctype">&lt;!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>mapper</span> <span class="token attr-name">namespace</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.dao.EmpDao<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>

    <span class="token comment" spellcheck="true">&lt;!--将每一个属性值都映射成对象中的数据，如果有实体类对象，就写成对象.属性的方式--></span>
    <span class="token comment" spellcheck="true">&lt;!--&lt;resultMap id="myEmp" type="com.cmz.bean.Emp">
        &lt;id column="empno" property="empno">&lt;/id>
        &lt;id column="ename" property="ename">&lt;/id>
        &lt;id column="mgr" property="mgr">&lt;/id>
        &lt;id column="hiredate" property="hiredate">&lt;/id>
        &lt;id column="sal" property="sal">&lt;/id>
        &lt;id column="comm" property="comm">&lt;/id>
        &lt;id column="deptno" property="dept.deptno">&lt;/id>
        &lt;id column="dname" property="dept.dname">&lt;/id>
        &lt;id column="loc" property="dept.loc">&lt;/id>
    &lt;/resultMap>
    &lt;select id="seletEmpByEmpno" resultMap="myEmp">
        select * from emp left join dept on emp.deptno=dept.deptno where empno = #{empno}
    &lt;/select>--></span>

    <span class="token comment" spellcheck="true">&lt;!--个人推荐第二种方式--></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>resultMap</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>myEmp<span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ename<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ename<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>mgr<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>mgr<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>hiredate<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>hiredate<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sal<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sal<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>comm<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>comm<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>association</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dept<span class="token punctuation">"</span></span> <span class="token attr-name">javaType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Dept<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dname<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dname<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>loc<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>loc<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>association</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>resultMap</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>seletEmpByEmpno<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>myEmp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        select * from emp left join dept on emp.deptno=dept.deptno where empno = #{empno}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByStep<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empDept<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        select * from emp where empno = #{empno}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>resultMap</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empDept<span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ename<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ename<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>mgr<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>mgr<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>hiredate<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>hiredate<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sal<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sal<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>comm<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>comm<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>association</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dept<span class="token punctuation">"</span></span> <span class="token attr-name">select</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.dao.DeptDao.selectDeptByStep<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>association</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>resultMap</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>mapper</span><span class="token punctuation">></span></span></code></pre>
<p>查询员工信息，加上部门[外链]</p>
<ul>
<li><p>方式1</p>
<p>将每一个属性值都映射成对象中的数据，如果有实体类对象，就写成对象.属性的方式</p>
<pre><code>&lt;!--将每一个属性值都映射成对象中的数据，如果有实体类对象，就写成对象.属性的方式--&gt;
    &lt;resultMap id=&quot;myEmp1&quot; type=&quot;com.cmz.bean.Emp&quot;&gt;
        &lt;id column=&quot;empno&quot; property=&quot;empno&quot;&gt;&lt;/id&gt;
        &lt;id column=&quot;ename&quot; property=&quot;ename&quot;&gt;&lt;/id&gt;
        &lt;id column=&quot;mgr&quot; property=&quot;mgr&quot;&gt;&lt;/id&gt;
        &lt;id column=&quot;hiredate&quot; property=&quot;hiredate&quot;&gt;&lt;/id&gt;
        &lt;id column=&quot;sal&quot; property=&quot;sal&quot;&gt;&lt;/id&gt;
        &lt;id column=&quot;comm&quot; property=&quot;comm&quot;&gt;&lt;/id&gt;
        &lt;id column=&quot;deptno&quot; property=&quot;dept.deptno&quot;&gt;&lt;/id&gt;
        &lt;id column=&quot;dname&quot; property=&quot;dept.dname&quot;&gt;&lt;/id&gt;
        &lt;id column=&quot;loc&quot; property=&quot;dept.loc&quot;&gt;&lt;/id&gt;
    &lt;/resultMap&gt;
    &lt;select id=&quot;seletEmpByEmpno1&quot; resultMap=&quot;myEmp1&quot;&gt;
        select * from emp left join dept on emp.deptno=dept.deptno where empno = #{empno}
    &lt;/select&gt;</code></pre><p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200415140501.png" alt="方式1"></p>
</li>
<li><p>方式2</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test01</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//获取与数据库相关的会话</span>
    SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//获取对应的映射接口对象</span>
    EmpDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>EmpDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    Emp emp <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">seletEmpByEmpno</span><span class="token punctuation">(</span><span class="token number">7369</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>emp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//关闭会话</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p><code>EmpDao.xml</code></p>
<pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!--个人推荐第二种方式--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>resultMap</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>myEmp<span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ename<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ename<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>mgr<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>mgr<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>hiredate<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>hiredate<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sal<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sal<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>comm<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>comm<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--associtaion 可以指定联合的javBean对象
    property="dept" 指定哪个属性是联合的对象
    javaType: 指定这个属性对象的类型
    --></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>association</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dept<span class="token punctuation">"</span></span> <span class="token attr-name">javaType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Dept<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dname<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dname<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>loc<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>loc<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>association</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>resultMap</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>seletEmpByEmpno<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>myEmp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    select * from emp left join dept on emp.deptno=dept.deptno where empno = #{empno}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200415154903.png" alt="方式2"></p>
</li>
</ul>
<p>输出</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: select * from emp left join dept on emp.deptno=dept.deptno where empno = ? 
DEBUG [main] - ==> Parameters: 7369(Integer)
TRACE [main] - <==    Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DEPTNO, DNAME, LOC
TRACE [main] - <==        Row: 7369, SMITH, CLERK, 7902.0, 1980-12-17, 800.0, null, 20, 20, RESEARCH, DALLAS
DEBUG [main] - <==      Total: 1
Emp{empno=7369, ename='SMITH', job='null', mgr=7902, hiredate=Wed Dec 17 08:00:00 CST 1980, sal=800.0, common=null, dept=Dept{deptno=20, dname='RESEARCH', loc='DALLAS', emps=null}}</code></pre>
<pre class=" language-sql"><code class="language-sql">mysql<span class="token operator">></span> <span class="token keyword">select</span> emp<span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">,</span>dept<span class="token punctuation">.</span><span class="token operator">*</span> <span class="token keyword">from</span> emp <span class="token keyword">left</span> <span class="token keyword">join</span> dept <span class="token keyword">on</span> emp<span class="token punctuation">.</span>deptno<span class="token operator">=</span>dept<span class="token punctuation">.</span>deptno <span class="token keyword">where</span> empno <span class="token operator">=</span> <span class="token number">7369</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment" spellcheck="true">-------+-------+-------+------+------------+------+------+--------+--------+----------+--------+</span>
<span class="token operator">|</span> EMPNO <span class="token operator">|</span> ENAME <span class="token operator">|</span> JOB   <span class="token operator">|</span> MGR  <span class="token operator">|</span> HIREDATE   <span class="token operator">|</span> SAL  <span class="token operator">|</span> COMM <span class="token operator">|</span> DEPTNO <span class="token operator">|</span> DEPTNO <span class="token operator">|</span> DNAME    <span class="token operator">|</span> LOC    <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment" spellcheck="true">-------+-------+-------+------+------------+------+------+--------+--------+----------+--------+</span>
<span class="token operator">|</span>  <span class="token number">7369</span> <span class="token operator">|</span> SMITH <span class="token operator">|</span> CLERK <span class="token operator">|</span> <span class="token number">7902</span> <span class="token operator">|</span> <span class="token number">1980</span><span class="token operator">-</span><span class="token number">12</span><span class="token operator">-</span><span class="token number">17</span> <span class="token operator">|</span>  <span class="token number">800</span> <span class="token operator">|</span> <span class="token boolean">NULL</span> <span class="token operator">|</span>     <span class="token number">20</span> <span class="token operator">|</span>     <span class="token number">20</span> <span class="token operator">|</span> RESEARCH <span class="token operator">|</span> DALLAS <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment" spellcheck="true">-------+-------+-------+------+------------+------+------+--------+--------+----------+--------+</span>
<span class="token number">1</span> <span class="token keyword">row</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>

mysql<span class="token operator">></span> <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> emp <span class="token keyword">where</span> empno <span class="token operator">=</span> <span class="token number">7369</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment" spellcheck="true">-------+-------+-------+------+------------+------+------+--------+</span>
<span class="token operator">|</span> EMPNO <span class="token operator">|</span> ENAME <span class="token operator">|</span> JOB   <span class="token operator">|</span> MGR  <span class="token operator">|</span> HIREDATE   <span class="token operator">|</span> SAL  <span class="token operator">|</span> COMM <span class="token operator">|</span> DEPTNO <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment" spellcheck="true">-------+-------+-------+------+------------+------+------+--------+</span>
<span class="token operator">|</span>  <span class="token number">7369</span> <span class="token operator">|</span> SMITH <span class="token operator">|</span> CLERK <span class="token operator">|</span> <span class="token number">7902</span> <span class="token operator">|</span> <span class="token number">1980</span><span class="token operator">-</span><span class="token number">12</span><span class="token operator">-</span><span class="token number">17</span> <span class="token operator">|</span>  <span class="token number">800</span> <span class="token operator">|</span> <span class="token boolean">NULL</span> <span class="token operator">|</span>     <span class="token number">20</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment" spellcheck="true">-------+-------+-------+------+------------+------+------+--------+</span>
<span class="token number">1</span> <span class="token keyword">row</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span></code></pre>
<blockquote>
<p>个人推荐此种方式</p>
</blockquote>
<h4 id="2-4-3-association-分步查询"><a href="#2-4-3-association-分步查询" class="headerlink" title="2.4.3 association 分步查询"></a>2.4.3 association 分步查询</h4><p>也就是sql分开查询。我们查询员工信息，顺带查询员工的部门</p>
<pre><code>public class Emp {
    private Integer empno;
    private String ename;
    private String job;
    private Integer mgr;
    private Date hiredate;
    private Double sal;
    private Double common;
    private Dept dept;</code></pre><blockquote>
<p>也就是查询emp的信息。连同他的dept也一起查询。</p>
</blockquote>
<p>`EmpDao.java</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> Emp <span class="token function">selectEmpByStep</span><span class="token punctuation">(</span>Integer empno<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p><code>EmpDao.xml</code></p>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByStep<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empDept<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    select * from emp where empno = #{empno}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--
    1. 先根据员工的id，查出员工信息
    2. 然后根据员工信息中的deptno的值去部门表查询出部门信息
    --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>resultMap</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empDept<span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ename<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ename<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>mgr<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>mgr<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>hiredate<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>hiredate<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sal<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sal<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>comm<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>comm<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>

    <span class="token comment" spellcheck="true">&lt;!--ssociation定义关联对象的封装原装
        select: 表明当前属性是调用select指定的方法查出的结果
        column: 指定哪一列的值传递给这个方法
        流程： 使用select指定的方法(传入column指定的这列参数的值)查出对象，并封装给property[也就是dept]指定的属性
    --></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>association</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dept<span class="token punctuation">"</span></span> <span class="token attr-name">select</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.dao.DeptDao.selectDeptByStep<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>association</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>resultMap</span><span class="token punctuation">></span></span></code></pre>
<p><code>MyTest.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test03</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//获取与数据库相关的会话</span>
    SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//获取对应的映射接口对象</span>
    EmpDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>EmpDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    Emp emp <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectEmpByStep</span><span class="token punctuation">(</span><span class="token number">7369</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>emp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//关闭会话</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>输出,分开两步查询了</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: select * from emp where empno = ? 
DEBUG [main] - ==> Parameters: 7369(Integer)
TRACE [main] - <==    Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
TRACE [main] - <==        Row: 7369, SMITH, CLERK, 7902.0, 1980-12-17, 800.0, null, 20
DEBUG [main] - ====>  Preparing: select * from dept where deptno = ? 
DEBUG [main] - ====> Parameters: 20(Integer)
TRACE [main] - <====    Columns: DEPTNO, DNAME, LOC
TRACE [main] - <====        Row: 20, RESEARCH, DALLAS
DEBUG [main] - <====      Total: 1
DEBUG [main] - <==      Total: 1
Emp{empno=7369, ename='SMITH', job='CLERK', mgr=7902, hiredate=Wed Dec 17 08:00:00 CST 1980, sal=800.0, common=null, dept=Dept{deptno=20, dname='RESEARCH', loc='DALLAS', emps=null}}</code></pre>
<p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200415162628.png" alt="分布查询"></p>
<p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200415163539.png" alt="关联查询"></p>
<blockquote>
<ol>
<li>先根据员工的id，查出员工信息</li>
<li>然后根据员工信息中的deptno的值去部门表查询出部门信息</li>
<li>流程： 使用select指定的方法(传入column指定的这列参数的值)查出对象，并封装给property[也就是dept]指定的属性</li>
</ol>
</blockquote>
<h4 id="2-4-4-association-延时加载"><a href="#2-4-4-association-延时加载" class="headerlink" title="2.4.4 association 延时加载"></a>2.4.4 association 延时加载</h4><p>所谓延时加载。</p>
<pre><code>Emp中包含Dept，我们每次查询Emp对象的时候，都将一起查询出来。
将部门信息在我们使用的时候再去查询，在分步查询基础上加两个配置即可完成，延时加载</code></pre><p><code>mybatis_config.xml</code></p>
<pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!--显示的指定每个我们需要更改的配置的值，即便他是默认的，防止版本更新代来的问题--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>setting</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>lazyLoadingEnabled<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>setting</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>aggressiveLazyLoading<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>false<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span></code></pre>
<ul>
<li><p>当我们只打印员工信息的时候，sql就执行一条</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test03</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//获取与数据库相关的会话</span>
    SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//获取对应的映射接口对象</span>
    EmpDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>EmpDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    Emp emp <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectEmpByStep</span><span class="token punctuation">(</span><span class="token number">7369</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>emp<span class="token punctuation">.</span><span class="token function">getEname</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//不打印部门，就不会去调用第二条sql</span>
    <span class="token comment" spellcheck="true">//关闭会话</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>输出</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: select * from emp where empno = ? 
DEBUG [main] - ==> Parameters: 7369(Integer)
TRACE [main] - <==    Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
TRACE [main] - <==        Row: 7369, SMITH, CLERK, 7902.0, 1980-12-17, 800.0, null, 20
DEBUG [main] - ====>  Preparing: select * from dept where deptno = ? 
DEBUG [main] - ====> Parameters: 20(Integer)
TRACE [main] - <====    Columns: DEPTNO, DNAME, LOC
TRACE [main] - <====        Row: 20, RESEARCH, DALLAS
DEBUG [main] - <====      Total: 1
DEBUG [main] - <==      Total: 1
SMITH</code></pre>
</li>
</ul>
<ul>
<li><p>若是打印员工部门的时候。就再执行一条了。也就是两条</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test03</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//获取与数据库相关的会话</span>
    SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//获取对应的映射接口对象</span>
    EmpDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>EmpDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    Emp emp <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectEmpByStep</span><span class="token punctuation">(</span><span class="token number">7369</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>emp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//关闭会话</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>输出</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: select * from emp where empno = ? 
DEBUG [main] - ==> Parameters: 7369(Integer)
TRACE [main] - <==    Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
TRACE [main] - <==        Row: 7369, SMITH, CLERK, 7902.0, 1980-12-17, 800.0, null, 20
DEBUG [main] - <==      Total: 1
DEBUG [main] - ==>  Preparing: select * from dept where deptno = ? 
DEBUG [main] - ==> Parameters: 20(Integer)
TRACE [main] - <==    Columns: DEPTNO, DNAME, LOC
TRACE [main] - <==        Row: 20, RESEARCH, DALLAS
DEBUG [main] - <==      Total: 1
Emp{empno=7369, ename='SMITH', job='CLERK', mgr=7902, hiredate=Wed Dec 17 08:00:00 CST 1980, sal=800.0, common=null, dept=Dept{deptno=20, dname='RESEARCH', loc='DALLAS', emps=null}}</code></pre>
<p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200415170856.gif" alt="懒加载"></p>
<blockquote>
<ol>
<li><p>开启懒加载就是在分步查询的时候区别，后期数据是否使用到，使用到就执行sql，不使用到数据就不执行sql，前提是在mybatis_config.xml中配置两个字段lazyLoadingEnabled和aggressiveLazyLoading</p>
</li>
<li><p>不开启懒加载的话，不管啥时候，sql都执行</p>
</li>
</ol>
</blockquote>
</li>
</ul>
<h4 id="2-4-5-collection"><a href="#2-4-5-collection" class="headerlink" title="2.4.5 collection"></a>2.4.5 collection</h4><p>collection嵌套结果集的方法，定义关联的结合类型元素的封装规则</p>
<p>我从部门表中查询出所有员工信息。</p>
<pre><code>public class Dept implements Serializable {
    private Integer deptno;
    private String dname;
    private String loc;
    private List&lt;Emp&gt; emps;</code></pre><blockquote>
<p>查询出某个部门下的所有员工信息。</p>
</blockquote>
<p><code>DeptDao.xml</code></p>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>resultMap</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>myDept<span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Dept<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dname<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dname<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>loc<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>loc<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--
        collection: 定义关联集合类型的属性的封装规则
        ofType: 指定集合里面元素的类型
    --></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>collection</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>emps<span class="token punctuation">"</span></span> <span class="token attr-name">ofType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ename<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ename<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>job<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>job<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>mgr<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>mgr<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>hiredate<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>hiredate<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sal<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sal<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>comm<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>comm<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>collection</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>resultMap</span><span class="token punctuation">></span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectDeptByDeptno<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>myDept<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
   select * from dept left join emp on dept.deptno = emp.deptno where dept.deptno = #{deptno}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<p><code>MyTest.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/*collection*/</span>
<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test02</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//获取与数据库相关的会话</span>
    SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//获取对应的映射接口对象</span>
    DeptDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>DeptDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    Dept dept <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectDeptByDeptno</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>dept<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//关闭会话</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200415174748.png" alt="collection"></p>
<p>输出</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: select * from dept left join emp on dept.deptno = emp.deptno where dept.deptno = ? 
DEBUG [main] - ==> Parameters: 10(Integer)
TRACE [main] - <==    Columns: DEPTNO, DNAME, LOC, EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
TRACE [main] - <==        Row: 10, ACCOUNTING, NEW YORK, 7782, CLARK, MANAGER, 7839.0, 1981-06-09, 2450.0, null, 10
TRACE [main] - <==        Row: 10, ACCOUNTING, NEW YORK, 7839, KING, PRESIDENT, null, 1981-11-17, 5000.0, null, 10
TRACE [main] - <==        Row: 10, ACCOUNTING, NEW YORK, 7934, MILLER, CLERK, 7782.0, 1982-01-23, 1300.0, null, 10
DEBUG [main] - <==      Total: 3
Dept{deptno=10, dname='ACCOUNTING', loc='NEW YORK', emps=[Emp{empno=7782, ename='CLARK', job='MANAGER', mgr=7839, hiredate=Tue Jun 09 08:00:00 CST 1981, sal=2450.0, common=null, dept=null}, Emp{empno=7839, ename='KING', job='PRESIDENT', mgr=null, hiredate=Tue Nov 17 08:00:00 CST 1981, sal=5000.0, common=null, dept=null}, Emp{empno=7934, ename='MILLER', job='CLERK', mgr=7782, hiredate=Sat Jan 23 08:00:00 CST 1982, sal=1300.0, common=null, dept=null}]}</code></pre>
<blockquote>
<p>emps 查出所有员工信息</p>
</blockquote>
<h4 id="2-4-6-collection分步查询"><a href="#2-4-6-collection分步查询" class="headerlink" title="2.4.6 collection分步查询"></a>2.4.6 collection分步查询</h4><p><code>DeptDao.xml</code></p>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectDeptByStep2<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptEmp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    select * from dept where deptno = #{deptno}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>resultMap</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptEmp<span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Dept<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dname<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dname<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>loc<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>loc<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--
    collection: 定义关联集合类型的属性的封装规则
    ofType: 指定集合里面元素的类型
    --></span>
    <span class="token comment" spellcheck="true">&lt;!--&lt;collection property="emps" ofType="com.cmz.bean.Emp" select="com.cmz.dao.EmpDao.selectEmpByStep2" column="deptno" fetchType="lazy">&lt;/collection>--></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>collection</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>emps<span class="token punctuation">"</span></span> <span class="token attr-name">ofType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span> <span class="token attr-name">select</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.dao.EmpDao.selectEmpByStep2<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>collection</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>resultMap</span><span class="token punctuation">></span></span></code></pre>
<p><code>EmpDao.xml</code></p>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByStep2<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    select * from emp where deptno = #{deptno}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<p><code>MyTest.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/*collection 分布式查询*/</span>
<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test05</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//获取与数据库相关的会话</span>
    SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//获取对应的映射接口对象</span>
    DeptDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>DeptDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    Dept dept <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectDeptByStep2</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>dept<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//关闭会话</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>输出结果</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: select * from dept where deptno = ? 
DEBUG [main] - ==> Parameters: 10(Integer)
TRACE [main] - <==    Columns: DEPTNO, DNAME, LOC
TRACE [main] - <==        Row: 10, ACCOUNTING, NEW YORK
DEBUG [main] - <==      Total: 1
DEBUG [main] - ==>  Preparing: select * from emp where deptno = ? 
DEBUG [main] - ==> Parameters: 10(Integer)
TRACE [main] - <==    Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
TRACE [main] - <==        Row: 7782, CLARK, MANAGER, 7839.0, 1981-06-09, 2450.0, null, 10
TRACE [main] - <==        Row: 7839, KING, PRESIDENT, null, 1981-11-17, 5000.0, null, 10
TRACE [main] - <==        Row: 7934, MILLER, CLERK, 7782.0, 1982-01-23, 1300.0, null, 10
DEBUG [main] - <==      Total: 3
Dept{deptno=10, dname='ACCOUNTING', loc='NEW YORK', emps=[Emp{empno=7782, ename='CLARK', job='MANAGER', mgr=7839, hiredate=Tue Jun 09 08:00:00 CST 1981, sal=2450.0, common=null, dept=null}, Emp{empno=7839, ename='KING', job='PRESIDENT', mgr=null, hiredate=Tue Nov 17 08:00:00 CST 1981, sal=5000.0, common=null, dept=null}, Emp{empno=7934, ename='MILLER', job='CLERK', mgr=7782, hiredate=Sat Jan 23 08:00:00 CST 1982, sal=1300.0, common=null, dept=null}]}</code></pre>
<blockquote>
<p>通过部门查询某个部门下的所有员工</p>
</blockquote>
<h4 id="2-4-7-collection懒加载"><a href="#2-4-7-collection懒加载" class="headerlink" title="2.4.7 collection懒加载"></a>2.4.7 collection懒加载</h4><p>和associate懒加载一样。</p>
<p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200415182554.png" alt="懒加载"></p>
<h4 id="2-4-8-fetchType"><a href="#2-4-8-fetchType" class="headerlink" title="2.4.8 fetchType"></a>2.4.8 fetchType</h4><pre><code>collection: 定义关联集合类型的属性的封装规则
ofType: 指定集合里面元素的类型
fetchType:
    lazy: 延时加载
    eager: 立即加载，即便开启了全局的延时加载，这样此处配置了立即加载还是会立即加载的</code></pre><h4 id="2-4-9-多参数传递"><a href="#2-4-9-多参数传递" class="headerlink" title="2.4.9 多参数传递"></a>2.4.9 多参数传递</h4><p>我们都是通过column配置将参数传递给下去。但是都是传递一个参数，我们如何传递多个参数呢。</p>
<pre class=" language-shell"><code class="language-shell">多列的值传递过去
将多列的值封装map传递
column=“{key1=column1,key2=column2}”</code></pre>
<p>例如</p>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>resultMap</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptEmp<span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Dept<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dname<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dname<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>loc<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>loc<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>result</span><span class="token punctuation">></span></span>
        <span class="token comment" spellcheck="true">&lt;!--
        collection: 定义关联集合类型的属性的封装规则
        ofType: 指定集合里面元素的类型
        fetchType:
            lazy: 延时加载
            eager: 立即加载
        --></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>collection</span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>emps<span class="token punctuation">"</span></span> <span class="token attr-name">ofType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span> <span class="token attr-name">select</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.dao.EmpDao.selectEmpByStep2<span class="token punctuation">"</span></span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>{deptno<span class="token punctuation">=</span>deptno}<span class="token punctuation">"</span></span> <span class="token attr-name">fetchType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>eager<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>collection</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>resultMap</span><span class="token punctuation">></span></span></code></pre>
<blockquote>
<p>column=”{deptno=deptno}” ,效果一样。</p>
</blockquote>
<h3 id="2-5"><a href="#2-5" class="headerlink" title="2.5 #{},${}"></a>2.5 <code>#{}</code>,<code>${}</code></h3><pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!--参数的获取值的方式：
每次在向sql语句中设置结果值的时候，可以使用#{}，还可以使用${}这样的方式，那么哪种比较好？
    #{}：select * from emp where empno = ?
    ${}: select * from emp where empno = 7369
    通过sql语句可以得出结论：
        #{}的处理方式是使用了参数预编译的方式，不会引起sql注入的问题
        ${}的处理方式是直接拼接sql语句，得到对应的sql语句，会有sql注入的危险
        因此，我们推荐大家使用#{}的方式
        但是要注意，${}也是有自己的使用场景的？
            当需要传入动态的表名，列名的时候就需要使用${},就是最直接的拼接字符串的行为

比如分表，排序，，，按照年份分表拆分

select * from ${year}_salary where xxx;
--></span></code></pre>
<h3 id="2-6-参数获取"><a href="#2-6-参数获取" class="headerlink" title="2.6 参数获取"></a>2.6 参数获取</h3><p>当查询语句中包含多个参数的时候，我们应该如何获取需要的参数</p>
<ul>
<li><p>如果是单个参数，</p>
<pre><code>基本数据类型：那么可以使用#{}随便获取
引用数据类型:使用#{}获取值的是必须要使用对象的属性名</code></pre><ul>
<li><p>基本数据类型</p>
<p>基本数据类型：那么可以使用#{参数值}随便获取，参数值，可以随便写，都可以获取到结果。</p>
<p>Emp中的empno类型改为基本数据类型<code>int</code>，Emp.class</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Emp</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">//    private Integer empno;</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> empno<span class="token punctuation">;</span>
    <span class="token keyword">private</span> String ename<span class="token punctuation">;</span>
    <span class="token keyword">private</span> String job<span class="token punctuation">;</span>
    <span class="token keyword">private</span> Integer mgr<span class="token punctuation">;</span>
    <span class="token keyword">private</span> Date hiredate<span class="token punctuation">;</span>
    <span class="token keyword">private</span> Double sal<span class="token punctuation">;</span>
    <span class="token keyword">private</span> Double common<span class="token punctuation">;</span>
    <span class="token keyword">private</span> Integer deptno<span class="token punctuation">;</span></code></pre>
<p>此时EmpDao.xml中参数可以随便写</p>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByEmpno<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span> <span class="token attr-name">databaseId</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>mysql<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    select * from emp where empno = #{empno}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<blockquote>
<p>正常我们写empno = #{empno},但是#{empno}中的empno随便是写啥都可以。</p>
</blockquote>
<p>测试</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test01</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">/*String resource = "mybatis_config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);*/</span>

    <span class="token comment" spellcheck="true">//获取与数据库相关的会话</span>
    SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//获取对应的映射接口对象</span>
    EmpDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>EmpDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//执行对应的sql</span>
    Emp emp <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectEmpByEmpno</span><span class="token punctuation">(</span><span class="token number">7369</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>emp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>输出</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: select * from emp where empno = ? 
DEBUG [main] - ==> Parameters: 7369(Integer)
TRACE [main] - <==    Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
TRACE [main] - <==        Row: 7369, SMITH, CLERK, 7902.0, 1980-12-17, 800.0, null, 20
DEBUG [main] - <==      Total: 1
Emp{empno=7369, ename='SMITH', job='CLERK', mgr=7902, hiredate=Wed Dec 17 08:00:00 CST 1980, sal=800.0, common=null, deptno=20}</code></pre>
<p>改为</p>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByEmpno<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    select * from emp where empno = #{xxxx}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<p>再次运行</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: select * from emp where empno = ? 
DEBUG [main] - ==> Parameters: 7369(Integer)
TRACE [main] - <==    Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
TRACE [main] - <==        Row: 7369, SMITH, CLERK, 7902.0, 1980-12-17, 800.0, null, 20
DEBUG [main] - <==      Total: 1
Emp{empno=7369, ename='SMITH', job='CLERK', mgr=7902, hiredate=Wed Dec 17 08:00:00 CST 1980, sal=800.0, common=null, deptno=20}</code></pre>
<p>结果还是一样输出。</p>
</li>
<li><p>引用数据类型</p>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByEmpnoAndSal<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    select * from emp where empno = #{empno} and sal > #{sal}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<p>EmpDao.class</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">EmpDao</span> <span class="token punctuation">{</span>

    <span class="token keyword">public</span> Integer <span class="token function">save</span><span class="token punctuation">(</span>Emp emp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> Integer <span class="token function">update</span><span class="token punctuation">(</span>Emp emp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> Integer <span class="token function">delete</span><span class="token punctuation">(</span>Integer empno<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> Emp <span class="token function">selectEmpByEmpno</span><span class="token punctuation">(</span>Integer empno<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> List<span class="token operator">&lt;</span>Emp<span class="token operator">></span> <span class="token function">selectAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> List<span class="token operator">&lt;</span>Emp<span class="token operator">></span> <span class="token function">selectEmpByEmpnoAndSal</span><span class="token punctuation">(</span>Emp emp<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>测试</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test09</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    EmpDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>EmpDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    Emp emp <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Emp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    emp<span class="token punctuation">.</span><span class="token function">setEmpno</span><span class="token punctuation">(</span><span class="token number">7369</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    emp<span class="token punctuation">.</span><span class="token function">setSal</span><span class="token punctuation">(</span><span class="token number">666.6</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    List<span class="token operator">&lt;</span>Emp<span class="token operator">></span> list <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectEmpByEmpnoAndSal</span><span class="token punctuation">(</span>emp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>Emp emp1 <span class="token operator">:</span> list<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>emp1<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>运行结果</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: select * from emp where empno = ? and sal >? 
DEBUG [main] - ==> Parameters: 7369(Integer), 666.6(Double)
TRACE [main] - <==    Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
TRACE [main] - <==        Row: 7369, SMITH, CLERK, 7902.0, 1980-12-17, 800.0, null, 20
DEBUG [main] - <==      Total: 1
Emp{empno=7369, ename='SMITH', job='CLERK', mgr=7902, hiredate=Wed Dec 17 08:00:00 CST 1980, sal=800.0, common=null, deptno=20}</code></pre>
<blockquote>
<p>此时是能正常获取到的</p>
</blockquote>
<hr>

<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByEmpnoAndSal<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    select * from emp where empno = #{empno} and sal > #{aaa}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<p>再次运行</p>
<pre class=" language-java"><code class="language-java">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>exceptions<span class="token punctuation">.</span>PersistenceException<span class="token operator">:</span> 
### Error querying database<span class="token punctuation">.</span>  Cause<span class="token operator">:</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>reflection<span class="token punctuation">.</span>ReflectionException<span class="token operator">:</span> There is no getter <span class="token keyword">for</span> property named <span class="token string">'aaa'</span> in <span class="token string">'class com.cmz.bean.Emp'</span>
### Cause<span class="token operator">:</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>reflection<span class="token punctuation">.</span>ReflectionException<span class="token operator">:</span> There is no getter <span class="token keyword">for</span> property named <span class="token string">'aaa'</span> in <span class="token string">'class com.cmz.bean.Emp'</span>

    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>exceptions<span class="token punctuation">.</span>ExceptionFactory<span class="token punctuation">.</span><span class="token function">wrapException</span><span class="token punctuation">(</span>ExceptionFactory<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">30</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>session<span class="token punctuation">.</span>defaults<span class="token punctuation">.</span>DefaultSqlSession<span class="token punctuation">.</span><span class="token function">selectList</span><span class="token punctuation">(</span>DefaultSqlSession<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">149</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>session<span class="token punctuation">.</span>defaults<span class="token punctuation">.</span>DefaultSqlSession<span class="token punctuation">.</span><span class="token function">selectList</span><span class="token punctuation">(</span>DefaultSqlSession<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">140</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>binding<span class="token punctuation">.</span>MapperMethod<span class="token punctuation">.</span><span class="token function">executeForMany</span><span class="token punctuation">(</span>MapperMethod<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">147</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>binding<span class="token punctuation">.</span>MapperMethod<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span>MapperMethod<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">80</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>binding<span class="token punctuation">.</span>MapperProxy$PlainMethodInvoker<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>MapperProxy<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">144</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>binding<span class="token punctuation">.</span>MapperProxy<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>MapperProxy<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">85</span><span class="token punctuation">)</span>
    at com<span class="token punctuation">.</span>sun<span class="token punctuation">.</span>proxy<span class="token punctuation">.</span>$Proxy9<span class="token punctuation">.</span><span class="token function">selectEmpByEmpnoAndSal</span><span class="token punctuation">(</span>Unknown Source<span class="token punctuation">)</span>
    at MyTest<span class="token punctuation">.</span><span class="token function">test09</span><span class="token punctuation">(</span>MyTest<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">153</span><span class="token punctuation">)</span>
    at sun<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>NativeMethodAccessorImpl<span class="token punctuation">.</span><span class="token function">invoke0</span><span class="token punctuation">(</span>Native Method<span class="token punctuation">)</span>
    at sun<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>NativeMethodAccessorImpl<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>NativeMethodAccessorImpl<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">62</span><span class="token punctuation">)</span>
    at sun<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>DelegatingMethodAccessorImpl<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>DelegatingMethodAccessorImpl<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">43</span><span class="token punctuation">)</span>
    at java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>Method<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>Method<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">498</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>model<span class="token punctuation">.</span>FrameworkMethod$<span class="token number">1</span><span class="token punctuation">.</span><span class="token function">runReflectiveCall</span><span class="token punctuation">(</span>FrameworkMethod<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">59</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>internal<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>model<span class="token punctuation">.</span>ReflectiveCallable<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span>ReflectiveCallable<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">12</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>model<span class="token punctuation">.</span>FrameworkMethod<span class="token punctuation">.</span><span class="token function">invokeExplosively</span><span class="token punctuation">(</span>FrameworkMethod<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">56</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>internal<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>statements<span class="token punctuation">.</span>InvokeMethod<span class="token punctuation">.</span><span class="token function">evaluate</span><span class="token punctuation">(</span>InvokeMethod<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">17</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>internal<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>statements<span class="token punctuation">.</span>RunBefores<span class="token punctuation">.</span><span class="token function">evaluate</span><span class="token punctuation">(</span>RunBefores<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">26</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>ParentRunner$<span class="token number">3</span><span class="token punctuation">.</span><span class="token function">evaluate</span><span class="token punctuation">(</span>ParentRunner<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">306</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>BlockJUnit4ClassRunner$<span class="token number">1</span><span class="token punctuation">.</span><span class="token function">evaluate</span><span class="token punctuation">(</span>BlockJUnit4ClassRunner<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">100</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>ParentRunner<span class="token punctuation">.</span><span class="token function">runLeaf</span><span class="token punctuation">(</span>ParentRunner<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">366</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>BlockJUnit4ClassRunner<span class="token punctuation">.</span><span class="token function">runChild</span><span class="token punctuation">(</span>BlockJUnit4ClassRunner<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">103</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>BlockJUnit4ClassRunner<span class="token punctuation">.</span><span class="token function">runChild</span><span class="token punctuation">(</span>BlockJUnit4ClassRunner<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">63</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>ParentRunner$<span class="token number">4</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span>ParentRunner<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">331</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>ParentRunner$<span class="token number">1</span><span class="token punctuation">.</span><span class="token function">schedule</span><span class="token punctuation">(</span>ParentRunner<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">79</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>ParentRunner<span class="token punctuation">.</span><span class="token function">runChildren</span><span class="token punctuation">(</span>ParentRunner<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">329</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>ParentRunner<span class="token punctuation">.</span>access$<span class="token function">100</span><span class="token punctuation">(</span>ParentRunner<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">66</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>ParentRunner$<span class="token number">2</span><span class="token punctuation">.</span><span class="token function">evaluate</span><span class="token punctuation">(</span>ParentRunner<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">293</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>ParentRunner$<span class="token number">3</span><span class="token punctuation">.</span><span class="token function">evaluate</span><span class="token punctuation">(</span>ParentRunner<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">306</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>ParentRunner<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span>ParentRunner<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">413</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runner<span class="token punctuation">.</span>JUnitCore<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span>JUnitCore<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">137</span><span class="token punctuation">)</span>
    at com<span class="token punctuation">.</span>intellij<span class="token punctuation">.</span>junit4<span class="token punctuation">.</span>JUnit4IdeaTestRunner<span class="token punctuation">.</span><span class="token function">startRunnerWithArgs</span><span class="token punctuation">(</span>JUnit4IdeaTestRunner<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">68</span><span class="token punctuation">)</span>
    at com<span class="token punctuation">.</span>intellij<span class="token punctuation">.</span>rt<span class="token punctuation">.</span>execution<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>IdeaTestRunner$Repeater<span class="token punctuation">.</span><span class="token function">startRunnerWithArgs</span><span class="token punctuation">(</span>IdeaTestRunner<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">47</span><span class="token punctuation">)</span>
    at com<span class="token punctuation">.</span>intellij<span class="token punctuation">.</span>rt<span class="token punctuation">.</span>execution<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>JUnitStarter<span class="token punctuation">.</span><span class="token function">prepareStreamsAndStart</span><span class="token punctuation">(</span>JUnitStarter<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">242</span><span class="token punctuation">)</span>
    at com<span class="token punctuation">.</span>intellij<span class="token punctuation">.</span>rt<span class="token punctuation">.</span>execution<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>JUnitStarter<span class="token punctuation">.</span><span class="token function">main</span><span class="token punctuation">(</span>JUnitStarter<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">70</span><span class="token punctuation">)</span>
Caused by<span class="token operator">:</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>reflection<span class="token punctuation">.</span>ReflectionException<span class="token operator">:</span> There is no getter <span class="token keyword">for</span> property named <span class="token string">'aaa'</span> in <span class="token string">'class com.cmz.bean.Emp'</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>reflection<span class="token punctuation">.</span>Reflector<span class="token punctuation">.</span><span class="token function">getGetInvoker</span><span class="token punctuation">(</span>Reflector<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">373</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>reflection<span class="token punctuation">.</span>MetaClass<span class="token punctuation">.</span><span class="token function">getGetInvoker</span><span class="token punctuation">(</span>MetaClass<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">163</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>reflection<span class="token punctuation">.</span>wrapper<span class="token punctuation">.</span>BeanWrapper<span class="token punctuation">.</span><span class="token function">getBeanProperty</span><span class="token punctuation">(</span>BeanWrapper<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">162</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>reflection<span class="token punctuation">.</span>wrapper<span class="token punctuation">.</span>BeanWrapper<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>BeanWrapper<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">49</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>reflection<span class="token punctuation">.</span>MetaObject<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span>MetaObject<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">122</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>executor<span class="token punctuation">.</span>BaseExecutor<span class="token punctuation">.</span><span class="token function">createCacheKey</span><span class="token punctuation">(</span>BaseExecutor<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">219</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>executor<span class="token punctuation">.</span>CachingExecutor<span class="token punctuation">.</span><span class="token function">createCacheKey</span><span class="token punctuation">(</span>CachingExecutor<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">146</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>executor<span class="token punctuation">.</span>CachingExecutor<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>CachingExecutor<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">82</span><span class="token punctuation">)</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>session<span class="token punctuation">.</span>defaults<span class="token punctuation">.</span>DefaultSqlSession<span class="token punctuation">.</span><span class="token function">selectList</span><span class="token punctuation">(</span>DefaultSqlSession<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">147</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token number">33</span> more
</code></pre>
<blockquote>
<p>报错了，此时因为是引用类型所以参数不能服了吧瞎写。</p>
</blockquote>
</li>
</ul>
</li>
</ul>
<ul>
<li>如果是多个参数：<pre><code>我们在获取参数值的时候，就不能简单的通过#{}来获取了,只能通过arg0,arg1,param1,param2...这样的方式来获取参数的值
原因在于，mybatis在传入多个参数的时候，会讲这些参数的结果封装到map结构中，在map中key值就是(arg0,arg1,...)
(param1,param2...),这种方式非常不友好，没有办法根据属性名来获取具体的参数值
如果想要使用参数的话，可以进行如下的设置：
public List&lt;Emp&gt; selectEmpByEmpnoAndSal2(@Param(&quot;empno&quot;) Integer empno, @Param(&quot;sal&quot;) Double sal);
    这样的方式其实是根据@Param来进行参数的获取</code></pre></li>
</ul>
<pre class=" language-xml"><code class="language-xml">EmpDao.xml
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByEmpnoAndSal2<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    select * from emp where empno = #{empno} and sal > #{sal}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<pre class=" language-java"><code class="language-java">EmpDao<span class="token punctuation">.</span><span class="token keyword">class</span>
<span class="token class-name">public</span> List<span class="token operator">&lt;</span>Emp<span class="token operator">></span> <span class="token function">selectEmpByEmpnoAndSal2</span><span class="token punctuation">(</span>Integer empno<span class="token punctuation">,</span>Double sal<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<pre class=" language-shell"><code class="language-shell">org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.binding.BindingException: Parameter 'empno' not found. Available parameters are [arg1, arg0, param1, param2]
### Cause: org.apache.ibatis.binding.BindingException: Parameter 'empno' not found. Available parameters are [arg1, arg0, param1, param2]</code></pre>
<p>改为</p>
<pre class=" language-java"><code class="language-java">EmpDao<span class="token punctuation">.</span><span class="token keyword">class</span>
<span class="token class-name">public</span> List<span class="token operator">&lt;</span>Emp<span class="token operator">></span> <span class="token function">selectEmpByEmpnoAndSal2</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Param</span><span class="token punctuation">(</span><span class="token string">"empno"</span><span class="token punctuation">)</span> Integer empno<span class="token punctuation">,</span> <span class="token annotation punctuation">@Param</span><span class="token punctuation">(</span><span class="token string">"sal"</span><span class="token punctuation">)</span> Double sal<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>再次运行就ok了</p>
<pre class=" language-shell"><code class="language-shell">DEBUG [main] - ==>  Preparing: select * from emp where empno = ? and sal > ? 
DEBUG [main] - ==> Parameters: 7369(Integer), 500.0(Double)
TRACE [main] - <==    Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
TRACE [main] - <==        Row: 7369, SMITH, CLERK, 7902.0, 1980-12-17, 800.0, null, 20
DEBUG [main] - <==      Total: 1
Emp{empno=7369, ename='SMITH', job='CLERK', mgr=7902, hiredate=Wed Dec 17 08:00:00 CST 1980, sal=800.0, common=null, deptno=20}</code></pre>
<ul>
<li>如果多个参数正好是我们业务逻辑的数据模型，不经常使用的话， 我们也可以直接传入map.</li>
</ul>
<p>#{key}，取出map中对应的值。</p>
<p><code>EmpDao.class</code>，<code>EmpDao.xml</code>，<code>MyTest.class</code>配置修改。</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> List<span class="token operator">&lt;</span>Emp<span class="token operator">></span> <span class="token function">selectEmpByEmpnoAndSal3</span><span class="token punctuation">(</span>Map<span class="token operator">&lt;</span>String<span class="token punctuation">,</span>Object<span class="token operator">></span> map<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByEmpnoAndSal3<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    select * from emp where empno = #{empno} and sal > #{sal}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<p>测试</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test11</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    SqlSession sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    EmpDao mapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>EmpDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    Map<span class="token operator">&lt;</span>String<span class="token punctuation">,</span>Object<span class="token operator">></span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token operator">&lt;</span>String<span class="token punctuation">,</span> Object<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"empno"</span><span class="token punctuation">,</span><span class="token number">7369</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"sal"</span><span class="token punctuation">,</span><span class="token number">500.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    List<span class="token operator">&lt;</span>Emp<span class="token operator">></span> list <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">selectEmpByEmpnoAndSal3</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>Emp emp <span class="token operator">:</span> list<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>emp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>输出</p>
<pre class=" language-shel"><code class="language-shel">DEBUG [main] - ==>  Preparing: select * from emp where empno = ? and sal > ? 
DEBUG [main] - ==> Parameters: 7369(Integer), 500.0(Double)
TRACE [main] - <==    Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
TRACE [main] - <==        Row: 7369, SMITH, CLERK, 7902.0, 1980-12-17, 800.0, null, 20
DEBUG [main] - <==      Total: 1
Emp{empno=7369, ename='SMITH', job='CLERK', mgr=7902, hiredate=Wed Dec 17 08:00:00 CST 1980, sal=800.0, common=null, deptno=20}</code></pre>
<ul>
<li><p>如果多个参数不是业务模型中的数据，但是经常要使用，推荐来编写一个TO[Transfer Object]数据对象。</p>
<pre><code>Page{
    int index;
    int size;
}</code></pre></li>
</ul>
<p>思考场景</p>
<pre><code>public List&lt;Emp&gt; selectEmpByEmpnoAndSal2(@Param(&quot;empno&quot;) Integer empno, Double sal);
--》取值  empno==》#{empno}或者#{param1}   sal==》#{param2}

public List&lt;Emp&gt; selectEmpByEmpnoAndSal2(Integer empno,Emp emp);
--》取值  empno==》#{param1}   sal==》#{param2.sal}

public List&lt;Emp&gt; selectEmpByEmpnoAndSal2(Integer empno,@Param(&quot;emp&quot;)Emp emp);
--》取值  empno==》#{param1}   sal==》#{emp.sal}

// 特别注意，如果是Collectuon[list,set]类型，或者数组，也会特殊处理
也就是传入的list或者数组封装到map中。key,Collection(collection),如果是List还可以使用这个key(),若是数组就封装成array。
public List&lt;Emp&gt; selectEmpById(List&lt;Integer&gt; ids);
--》取值  取出第一个id的值  #{list[0]}</code></pre><h2 id="3-动态sql"><a href="#3-动态sql" class="headerlink" title="3 动态sql"></a>3 动态sql</h2><p>动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架，你应该能理解根据不同条件拼接 SQL 语句有多痛苦，例如拼接时要确保不能忘记添加必要的空格，还要注意去掉列表最后一个列名的逗号。利用动态 SQL，可以彻底摆脱这种痛苦。</p>
<h3 id="3-1-if"><a href="#3-1-if" class="headerlink" title="3.1 if"></a>3.1 if</h3><p>使用动态 SQL 最常见情景是根据条件包含 where 子句的一部分</p>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByCondition<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    select * from emp
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>where</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno!<span class="token punctuation">=</span>null<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
            empno > #{empno}
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>if</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>where</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<p>这条语句提供了可选的查找文本功能。如果不传入 “empno”，那么所有处于 “null” 状态的 emp都会返回；如果传入了 “empno” 参数，那么就会对 “empno” 一列进行模糊查找并返回对应的 emp结果（细心的读者可能会发现，“empno” 的参数值需要包含查找掩码或通配符字符）。</p>
<p>如果希望通过 “empno” 和 “ename” 两个参数进行可选搜索该怎么办呢？首先，我想先将语句名称修改成更名副其实的名称；接下来，只需要加入另一个条件即可。</p>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByCondition<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    select * from emp
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>where</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno!<span class="token punctuation">=</span>null<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
            empno > #{empno}
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>if</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ename!<span class="token punctuation">=</span>null<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
            and ename=#{ename}
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>if</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sal <span class="token punctuation">></span> 500<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
            and sal > #{sal}
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>if</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>where</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<blockquote>
<p>使用where的时候，需要将连接的and写在最前面，若是次没匹配到话，会自动删除</p>
</blockquote>
<p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200416162252.png" alt="动态sql-if"></p>
<h3 id="3-2-choose、when、otherwise"><a href="#3-2-choose、when、otherwise" class="headerlink" title="3.2 choose、when、otherwise"></a>3.2 choose、when、otherwise</h3><p>有时候，我们不想使用所有的条件，而只是想从多个条件中选择一个使用。针对这种情况，MyBatis 提供了 choose 元素，它有点像 Java 中的 switch 语句。</p>
<pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!----></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByCondition<span class="token punctuation">"</span></span>
        <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    select * from emp
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>where</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>choose</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>when</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno!<span class="token punctuation">=</span>null<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
                empno = #{empno}
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>when</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>when</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ename!<span class="token punctuation">=</span>null<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
                ename = #{ename}
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>when</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>otherwise</span><span class="token punctuation">></span></span>
                sal > #{sal}
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>otherwise</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>choose</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>where</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<p>策略变为：传入了 “empno” 就按 “empno” 查找，传入了 “ename” 就按 “ename” 查找的情形。若两者都没有传入，就根据sal的参数做查找。</p>
<p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200416163900.png" alt="选择查找"></p>
<blockquote>
<p>也就是类型c++的switch，只要有匹配到就break了。</p>
</blockquote>
<h3 id="3-3-trim"><a href="#3-3-trim" class="headerlink" title="3.3 trim"></a>3.3 trim</h3><p>和where类似，定制where。</p>
<pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!--动态sql
trim:截取字符串，可以自定义where的格式
prefix：为sql语句整体添加一个前缀
prefixOverrides:去除整体sql语句前面多余的字符串
suffixOverriede:去除整体sql语句后面多余的字符串
    若是想去掉多个字符，中间可以使用|连接
--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByCondition<span class="token punctuation">"</span></span>
        <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    select * from emp
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>trim</span> <span class="token attr-name">prefix</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>where<span class="token punctuation">"</span></span> <span class="token attr-name">prefixOverrides</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>and<span class="token punctuation">"</span></span> <span class="token attr-name">suffixOverrides</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>and | or<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno!<span class="token punctuation">=</span>null<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
            empno = #{empno} and
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>if</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ename!<span class="token punctuation">=</span>null<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
            ename = #{ename} or
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>if</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>trim</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200416165229.png" alt="trim"></p>
<h3 id="3-4-foreach"><a href="#3-4-foreach" class="headerlink" title="3.4 foreach"></a>3.4 foreach</h3><p>动态 SQL 的另一个常见使用场景是对集合进行遍历（尤其是在构建 IN 条件语句的时候）</p>
<pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!--动态sql的foreach
foreach:遍历集合中的元素
    collection:指定要遍历的集合
    separator:分隔符
    open:以什么开始
    close：以什么结束
    item:遍历过程中的每一个元素值
    index:表示索引
--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectEmpByDeptnos<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    select * from emp where  deptno in
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>foreach</span> <span class="token attr-name">collection</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptnos<span class="token punctuation">"</span></span> <span class="token attr-name">separator</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>,<span class="token punctuation">"</span></span> <span class="token attr-name">open</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>(<span class="token punctuation">"</span></span> <span class="token attr-name">item</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span> <span class="token attr-name">index</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>idx<span class="token punctuation">"</span></span> <span class="token attr-name">close</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>)<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        #{deptno}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>foreach</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span></code></pre>
<p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200416172808.png" alt="foreach"></p>
<h2 id="4-缓存"><a href="#4-缓存" class="headerlink" title="4. 缓存"></a>4. 缓存</h2><p>知识点复习：</p>
<pre class=" language-shell"><code class="language-shell">mybatis中的缓存机制：
    如果没有缓存，那么每次查询的时候都需要从数据库中加载数据，这回造成io的性能问题，所以，在很多情况下
    如果连续执行两条相同的sql语句，可以直接从缓存中获取，如果获取不到，那么再去查询数据库，这意味着查询完成的结果
    需要放到缓存中。

    缓存分类：
        1、一级缓存：表示sqlSession级别的缓存，每次查询的时候会开启一个会话，此会话相当于一次连接，关闭之后自动失效
        2、二级缓存：全局范围内的缓存，sqlsession关闭之后才会生效
        3、第三方缓存：继承第三方的组件，来充当缓存的作用
    ==================
    一级缓存：表示将数据存储在sqlsession中，关闭之后自动失效，默认情况下是开启的
        在同一个会话之内，如果执行了多个相同的sql语句，那么除了第一个之外，所有的数据都是从缓存中进行查询的

        在某些情况下，一级缓存可能会失效？
            1、在同一个方法中，可能会开启多个会话，此时需要注意，会话跟方法没有关系，不是一个方法就只能由一个会话，所以严格
                记住,缓存的数据是保存在sqlsession中的
            2、当传递对象的时候，如果对象中的属性值不同，也不会走缓存
            3、在同一个连接中，如果修改了数据，那么缓存会失效，不同连接之间是不受影响的
            4、如果在一个会话过程中，手动清空了缓存，那么缓存也会失效
    二级缓存：表示的是全局缓存，必须要等到sqlsession关闭之后才会生效
        默认是不开启的，如果需要开启的话，需要进行如下设置
            1、修改全局配置文件，在settings中添加配置
                <setting name="cacheEnabled" value="true"/>
            2、指定在哪个映射文件中使用缓存的配置
                <cache></cache>
            3、对应的java实体类必须要实现序列化的接口
        在使用二级缓存的时候，可以包含多个属性值：
            eviction：缓存淘汰机制：
                    LRU： 最近最少使用
                    FIFO:先进先出，按照添加缓存的顺序执行
                    SOFT – 软引用：基于垃圾回收器状态和软引用规则移除对象。
                    WEAK – 弱引用：更积极地基于垃圾收集器状态和弱引用规则移除对象。
            flushInterval：设置多长时间进行缓存刷新
            size:引用的条数，是一个正整数，缓存中可以存储多少个对象，一般不设置，如果设置的话不要太大，会导致内存溢出
            readonly:只读属性：
                    true:只读缓存，会给所有的调用的方法返回该对象的实例，不安全
                    false:读写缓存，只是返回缓存对象的拷贝，比较安全

            一级缓存跟二级缓存有没有可能同时存在数据？
                    不会同时存在，因为二级缓存生效的时候，是在sqlsession关闭的时候
            当查询数据的时候，我们是先查询一级缓存还是先查询二级缓存？
                    先查询二级缓存，然后再查询一级缓存</code></pre>
<h3 id="4-1-缓存机制"><a href="#4-1-缓存机制" class="headerlink" title="4.1 缓存机制"></a>4.1 缓存机制</h3><p>&emsp;&emsp;如果没有缓存，那么每次查询的时候都需要从数据库中加载数据，这回造成io的性能问题，所以，在很多情况下如果连续执行两条相同的sql语句，可以直接从缓存中获取，如果获取不到，那么再去查询数据库，这意味着查询完成的结果需要放到缓存中。</p>
<h3 id="4-2-缓存分类"><a href="#4-2-缓存分类" class="headerlink" title="4.2 缓存分类"></a>4.2 缓存分类</h3><ul>
<li>一级缓存：表示sqlSession级别的缓存，每次查询的时候会开启一个会话，此会话相当于一次连接，关闭之后自动失效。一级缓存默认是开启的。</li>
<li>二级缓存：全局范围内的缓存，sqlsession关闭之后才会生效。默认不开启。</li>
<li>第三方缓存：继承第三方的组件，来充当缓存的作用</li>
</ul>
<h4 id="4-2-1-一级缓存"><a href="#4-2-1-一级缓存" class="headerlink" title="4.2.1 一级缓存"></a>4.2.1 一级缓存</h4><p>一级缓存：表示将数据存储在sqlsession中，关闭之后自动失效，默认情况下是开启的,在同一个会话之内，如果执行了多个相同的sql语句，那么除了第一个之外，所有的数据都是从缓存中进行查询的,在某些情况下，一级缓存可能会失效？</p>
<ul>
<li>在同一个方法中，可能会开启多个会话，此时需要注意，会话跟方法没有关系，不是一个方法就只能由一个会话，所以严格。记住,缓存的数据是保存在sqlsession中的</li>
<li>当传递对象的时候，如果对象中的属性值不同，也不会走缓存</li>
<li>在同一个连接中，如果修改了数据，那么缓存会失效，不同连接之间是不受影响的</li>
<li>如果在一个会话过程中，手动清空了缓存，那么缓存也会失效</li>
</ul>
<h4 id="4-2-2-二级缓存"><a href="#4-2-2-二级缓存" class="headerlink" title="4.2.2 二级缓存"></a>4.2.2 二级缓存</h4><p>二级缓存：表示的是<strong>全局缓存</strong>，必须要等到sqlsession关闭之后才会生效，默认是不开启的，如果需要开启的话，需要进行如下设置</p>
<ul>
<li>修改全局配置文件<code>mybatis_config.xml</code>，在settings中添加配置</li>
</ul>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>setting</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>cacheEnabled<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span></code></pre>
<ul>
<li>指定在哪个映射文件中使用缓存的配置，需要在哪个类上使用就加载哪个类的xml上</li>
</ul>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>cache</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>cache</span><span class="token punctuation">></span></span></code></pre>
<ul>
<li><p>对应的java实体类必须要实现序列化的接口</p>
<p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200417165950.png" alt="序列化"></p>
</li>
</ul>
<p>否则报错</p>
<pre class=" language-shell"><code class="language-shell">org.apache.ibatis.cache.CacheException: Error serializing object.  Cause: java.io.NotSerializableException: com.cmz.bean.Emp</code></pre>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/*二级缓存测试*/</span>
<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test14</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span>
    SqlSession sqlSession1 <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    SqlSession sqlSession2 <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    EmpDao mapper1 <span class="token operator">=</span> sqlSession1<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>EmpDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    EmpDao mapper2 <span class="token operator">=</span> sqlSession2<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>EmpDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    Emp emp1 <span class="token operator">=</span> mapper1<span class="token punctuation">.</span><span class="token function">seletEmpByEmpno</span><span class="token punctuation">(</span><span class="token number">7369</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>emp1<span class="token punctuation">)</span><span class="token punctuation">;</span>
    sqlSession1<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment" spellcheck="true">// 关闭后一级缓存已经失效了</span>

    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"-----------------"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    Emp emp2 <span class="token operator">=</span> mapper2<span class="token punctuation">.</span><span class="token function">seletEmpByEmpno</span><span class="token punctuation">(</span><span class="token number">7369</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>emp2<span class="token punctuation">)</span><span class="token punctuation">;</span>
    sqlSession2<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>运行结果</p>
<pre><code>DEBUG [main] - Cache Hit Ratio [com.cmz.dao.EmpDao]: 0.0
DEBUG [main] - ==&gt;  Preparing: select * from emp left join dept on emp.deptno=dept.deptno where empno = ? 
DEBUG [main] - ==&gt; Parameters: 7369(Integer)
TRACE [main] - &lt;==    Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DEPTNO, DNAME, LOC
TRACE [main] - &lt;==        Row: 7369, SMITH, CLERK, 7902.0, 1980-12-17, 800.0, null, 20, 20, RESEARCH, DALLAS
DEBUG [main] - &lt;==      Total: 1
Emp{empno=7369, ename=&#39;SMITH&#39;, job=&#39;null&#39;, mgr=7902, hiredate=Wed Dec 17 08:00:00 CST 1980, sal=800.0, common=null, dept=Dept{deptno=20, dname=&#39;RESEARCH&#39;, loc=&#39;DALLAS&#39;, emps=null}}
-----------------
DEBUG [main] - Cache Hit Ratio [com.cmz.dao.EmpDao]: 0.5
Emp{empno=7369, ename=&#39;SMITH&#39;, job=&#39;null&#39;, mgr=7902, hiredate=Wed Dec 17 08:00:00 CST 1980, sal=800.0, common=null, dept=Dept{deptno=20, dname=&#39;RESEARCH&#39;, loc=&#39;DALLAS&#39;, emps=null}}</code></pre><p>此时可以看出在一级缓存失效后，并没有去执行sql也就是直接从二级缓存中拿数据了。<code>DEBUG [main] - Cache Hit Ratio [com.cmz.dao.EmpDao]: 0.5</code>命中率也能看出来。</p>
<h3 id="4-3-缓存淘汰"><a href="#4-3-缓存淘汰" class="headerlink" title="4.3 缓存淘汰"></a>4.3 缓存淘汰</h3><pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>cache</span>
  <span class="token attr-name">eviction</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>FIFO<span class="token punctuation">"</span></span>
  <span class="token attr-name">flushInterval</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>60000<span class="token punctuation">"</span></span>
  <span class="token attr-name">size</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>512<span class="token punctuation">"</span></span>
  <span class="token attr-name">readOnly</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span></code></pre>
<p>这个更高级的配置创建了一个 FIFO 缓存，每隔 60 秒刷新，最多可以存储结果对象或列表的 512 个引用，而且返回的对象被认为是只读的，因此对它们进行修改可能会在不同线程中的调用者产生冲突。</p>
<p>在使用二级缓存的时候，可以包含多个属性值：</p>
<ul>
<li>eviction：缓存淘汰机制：<ul>
<li>LRU –  最近最少使用: 移除最长时间不被使用的对象   [默认是该配置]</li>
<li>FIFO – 先进先出: 按对象进入缓存的顺序来移除它们</li>
<li>SOFT – 软引用：基于垃圾回收器状态和软引用规则移除对象。</li>
<li>WEAK – 弱引用：更积极地基于垃圾收集器状态和弱引用规则移除对象。</li>
</ul>
</li>
<li>flushInterval：刷新间隔，属性可以被设置为任意的正整数，设置的值应该是一个以毫秒为单位的合理时间量。 默认情况是不设置，也就是没有刷新间隔，缓存仅仅会在调用语句时刷新。</li>
<li>size:引用的条数，是一个正整数，缓存中可以存储多少个对象，一般不设置，如果设置的话不要太大，会导致内存溢出，属性可以被设置为任意正整数，要注意欲缓存对象的大小和运行环境中可用的内存资源。默认值是 1024。</li>
<li>readonly:只读属性：<ul>
<li>true: 只读缓存，会给所有的调用的方法返回该对象的实例，不安全</li>
<li>false:读写缓存，只是返回缓存对象的拷贝，比较安全 ，【默认是该配置】</li>
</ul>
</li>
</ul>
<blockquote>
<p><strong>提示</strong> 二级缓存是事务性的。这意味着，当 SqlSession 完成并提交时，或是完成并回滚，但没有执行 flushCache=true 的 insert/delete/update 语句时，缓存会获得更新</p>
</blockquote>
<pre class=" language-xml"><code class="language-xml">Q: 一级缓存跟二级缓存有没有可能同时存在数据？
A: 不会同时存在，因为二级缓存生效的时候，是在sqlsession关闭的时候

Q: 当查询数据的时候，我们是先查询一级缓存还是先查询二级缓存？
A: 先查询二级缓存，然后再查询一级缓存</code></pre>
<p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200417231106.png" alt="缓存"></p>
<h3 id="4-4-第三方缓存"><a href="#4-4-第三方缓存" class="headerlink" title="4.4 第三方缓存"></a>4.4 第三方缓存</h3><p>在某些情况下我们也可以自定义实现缓存，或为其他第三方缓存方案创建适配器，来完全覆盖缓存行为。</p>
<ul>
<li>导入对应的maven依赖【pom.xml】</li>
</ul>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.mybatis.caches<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>mybatis-ehcache<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.2.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>

<span class="token comment" spellcheck="true">&lt;!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.slf4j<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>slf4j-api<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.0.0-alpha1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
<span class="token comment" spellcheck="true">&lt;!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.slf4j<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>slf4j-log4j12<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.0.0-alpha1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">></span></span>test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></code></pre>
<ul>
<li><p>导入ehcache配置文件</p>
<p>在resources下新建<code>ehcache.xml</code> [名字别瞎写]</p>
</li>
</ul>
<pre class=" language-xml"><code class="language-xml"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8"?></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ehcache</span> <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span>
<span class="token attr-name"><span class="token namespace">xsi:</span>noNamespaceSchemaLocation</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://ehcache.org/ehcache.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token comment" spellcheck="true">&lt;!-- 磁盘保存路径 --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>diskStore</span> <span class="token attr-name">path</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>D:\ehcache<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>defaultCache</span> 
 <span class="token attr-name">maxElementsInMemory</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>1<span class="token punctuation">"</span></span> 
 <span class="token attr-name">maxElementsOnDisk</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>10000000<span class="token punctuation">"</span></span>
 <span class="token attr-name">eternal</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>false<span class="token punctuation">"</span></span> 
 <span class="token attr-name">overflowToDisk</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span> 
 <span class="token attr-name">timeToIdleSeconds</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>120<span class="token punctuation">"</span></span>
 <span class="token attr-name">timeToLiveSeconds</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>120<span class="token punctuation">"</span></span> 
 <span class="token attr-name">diskExpiryThreadIntervalSeconds</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>120<span class="token punctuation">"</span></span>
 <span class="token attr-name">memoryStoreEvictionPolicy</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>LRU<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>defaultCache</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ehcache</span><span class="token punctuation">></span></span>

<span class="token comment" spellcheck="true">&lt;!-- 
属性说明：
l diskStore：指定数据在磁盘中的存储位置。
l defaultCache：当借助CacheManager.add("demoCache")创建Cache时，EhCache便会采用&lt;defalutCache/>指定的的管理策略

以下属性是必须的：
l maxElementsInMemory - 在内存中缓存的element的最大数目 
l maxElementsOnDisk - 在磁盘上缓存的element的最大数目，若是0表示无穷大
l eternal - 设定缓存的elements是否永远不过期。如果为true，则缓存的数据始终有效，如果为false那么还要根据timeToIdleSeconds，timeToLiveSeconds判断
l overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上

以下属性是可选的：
l timeToIdleSeconds - 当缓存在EhCache中的数据前后两次访问的时间超过timeToIdleSeconds的属性取值时，这些数据便会删除，默认值是0,也就是可闲置时间无穷大
l timeToLiveSeconds - 缓存element的有效生命期，默认是0.,也就是element存活时间无穷大
diskSpoolBufferSizeMB 这个参数设置DiskStore(磁盘缓存)的缓存区大小.默认是30MB.每个Cache都应该有自己的一个缓冲区.
l diskPersistent - 在VM重启的时候是否启用磁盘保存EhCache中的数据，默认是false。
l diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔，默认是120秒。每个120s，相应的线程会进行一次EhCache中数据的清理工作
l memoryStoreEvictionPolicy - 当内存缓存达到最大，有新的element加入的时候， 移除缓存中element的策略。默认是LRU（最近最少使用），可选的有LFU（最不常使用）和FIFO（先进先出）
--></span></code></pre>
<ul>
<li>在mapper文件中添加自定义缓存【EmpDao.xml】</li>
</ul>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>cache</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>org.mybatis.caches.ehcache.EhcacheCache<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>cache</span><span class="token punctuation">></span></span></code></pre>
<p>运行测试代码后</p>
<p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200417234321.png" alt="image-20200417234320113"></p>
<h2 id="5-逆向工程"><a href="#5-逆向工程" class="headerlink" title="5. 逆向工程"></a>5. 逆向工程</h2><h3 id="5-1-MyBatis3Simple"><a href="#5-1-MyBatis3Simple" class="headerlink" title="5.1 MyBatis3Simple"></a>5.1 MyBatis3Simple</h3><p><img src="https://gitee.com/caimengzhi/picBlog/raw/master///20200418003253.png" alt="逆向工程"></p>
<ul>
<li>pom.xml配置</li>
</ul>
<pre class=" language-xm"><code class="language-xm"><?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cmz</groupId>
    <artifactId>mybatis_generator</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mybatis.caches</groupId>
            <artifactId>mybatis-ehcache</artifactId>
            <version>1.2.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.0-alpha1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>2.0.0-alpha1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.4.0</version>
        </dependency>
    </dependencies>
</project></code></pre>
<ul>
<li>mbg.xml</li>
</ul>
<pre class=" language-xml"><code class="language-xml"><span class="token doctype">&lt;!DOCTYPE generatorConfiguration PUBLIC
        "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"></span>
<span class="token comment" spellcheck="true">&lt;!--总配置标签--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>generatorConfiguration</span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--具体配置的上下文环境--></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>context</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>simple<span class="token punctuation">"</span></span> <span class="token attr-name">targetRuntime</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>MyBatis3Simple<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        <span class="token comment" spellcheck="true">&lt;!--指向我们需要连接的数据库--></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>jdbcConnection</span> <span class="token attr-name">driverClass</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.mysql.cj.jdbc.Driver<span class="token punctuation">"</span></span>
                        <span class="token attr-name">connectionURL</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>jdbc:mysql://127.0.0.1:3306/demo?serverTimezone<span class="token punctuation">=</span>UTC<span class="token punctuation">"</span></span>
                        <span class="token attr-name">userId</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>root<span class="token punctuation">"</span></span> <span class="token attr-name">password</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>root<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>

        <span class="token comment" spellcheck="true">&lt;!--生成对应的实体类
        targetPackage:指定存放的包
        targetProject：指定当前工程的目录
        --></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>javaModelGenerator</span> <span class="token attr-name">targetPackage</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean<span class="token punctuation">"</span></span> <span class="token attr-name">targetProject</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>src/main/java<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>

        <span class="token comment" spellcheck="true">&lt;!--生成对应的SQL映射文件--></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>sqlMapGenerator</span> <span class="token attr-name">targetPackage</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.dao<span class="token punctuation">"</span></span> <span class="token attr-name">targetProject</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>src/main/resources<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>

        <span class="token comment" spellcheck="true">&lt;!--生成对应的DAO接口--></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>javaClientGenerator</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>XMLMAPPER<span class="token punctuation">"</span></span> <span class="token attr-name">targetPackage</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.dao<span class="token punctuation">"</span></span> <span class="token attr-name">targetProject</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>src/main/java<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>

        <span class="token comment" spellcheck="true">&lt;!--指定需要反向生成的表--></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>table</span> <span class="token attr-name">tableName</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>emp<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>table</span> <span class="token attr-name">tableName</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dept<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>context</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>generatorConfiguration</span><span class="token punctuation">></span></span></code></pre>
<ul>
<li>mybatis-config.xml</li>
</ul>
<pre class=" language-xml"><code class="language-xml"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8" ?></span>
<span class="token doctype">&lt;!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd"></span>
<span class="token comment" spellcheck="true">&lt;!--在填写标签的时候一定要注意相关配置的顺序--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>configuration</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>properties</span> <span class="token attr-name">resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>db.properties<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>properties</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>environments</span> <span class="token attr-name">default</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>development<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>environment</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>development<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>transactionManager</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>JDBC<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dataSource</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>POOLED<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>driver<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>${driver}<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>url<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>${url}<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>username<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>${username}<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>password<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>${password}<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dataSource</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>environment</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>environments</span><span class="token punctuation">></span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>configuration</span><span class="token punctuation">></span></span></code></pre>
<ul>
<li>log4j.properties</li>
</ul>
<pre class=" language-log"><code class="language-log"># 全局日志配置
log4j.rootLogger=INFO, stdout
# MyBatis 日志配置
log4j.logger.com.cmz=TRACE
# 控制台输出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n</code></pre>
<ul>
<li>db.properties</li>
</ul>
<pre><code>driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/demo?serverTimezone=UTC
username=root
password=root</code></pre><ul>
<li>MyTest.class</li>
</ul>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">;</span>

<span class="token keyword">import</span> org<span class="token punctuation">.</span>mybatis<span class="token punctuation">.</span>generator<span class="token punctuation">.</span>api<span class="token punctuation">.</span>MyBatisGenerator<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>mybatis<span class="token punctuation">.</span>generator<span class="token punctuation">.</span>config<span class="token punctuation">.</span>Configuration<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>mybatis<span class="token punctuation">.</span>generator<span class="token punctuation">.</span>config<span class="token punctuation">.</span>xml<span class="token punctuation">.</span>ConfigurationParser<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>mybatis<span class="token punctuation">.</span>generator<span class="token punctuation">.</span>internal<span class="token punctuation">.</span>DefaultShellCallback<span class="token punctuation">;</span>

<span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>File<span class="token punctuation">;</span>
<span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>ArrayList<span class="token punctuation">;</span>
<span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>List<span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/**
 * @author summer
 * @create 2020-04-18 0:05
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyTest</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> Exception <span class="token punctuation">{</span>
        List<span class="token operator">&lt;</span>String<span class="token operator">></span> warnings <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator">&lt;</span>String<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">boolean</span> overwrite <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
        File configFile <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string">"mbg.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        ConfigurationParser cp <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ConfigurationParser</span><span class="token punctuation">(</span>warnings<span class="token punctuation">)</span><span class="token punctuation">;</span>
        Configuration config <span class="token operator">=</span> cp<span class="token punctuation">.</span><span class="token function">parseConfiguration</span><span class="token punctuation">(</span>configFile<span class="token punctuation">)</span><span class="token punctuation">;</span>
        DefaultShellCallback callback <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DefaultShellCallback</span><span class="token punctuation">(</span>overwrite<span class="token punctuation">)</span><span class="token punctuation">;</span>
        MyBatisGenerator myBatisGenerator <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MyBatisGenerator</span><span class="token punctuation">(</span>config<span class="token punctuation">,</span> callback<span class="token punctuation">,</span> warnings<span class="token punctuation">)</span><span class="token punctuation">;</span>
        myBatisGenerator<span class="token punctuation">.</span><span class="token function">generate</span><span class="token punctuation">(</span>null<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<p>运行后</p>
<p><img src="C:%5CUsers%5Ccmz%5CDesktop%5Cmybatis%5Cupload%5Cimage-20200418001357896.png" alt="自动生成的文件"></p>
<p>生成的文件如下:</p>
<p><code>Dept.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>bean<span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Dept</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">/**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column dept.DEPTNO
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">private</span> Integer deptno<span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column dept.DNAME
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">private</span> String dname<span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column dept.LOC
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">private</span> String loc<span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column dept.DEPTNO
     *
     * @return the value of dept.DEPTNO
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> Integer <span class="token function">getDeptno</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> deptno<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column dept.DEPTNO
     *
     * @param deptno the value for dept.DEPTNO
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setDeptno</span><span class="token punctuation">(</span>Integer deptno<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>deptno <span class="token operator">=</span> deptno<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column dept.DNAME
     *
     * @return the value of dept.DNAME
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> String <span class="token function">getDname</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> dname<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column dept.DNAME
     *
     * @param dname the value for dept.DNAME
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setDname</span><span class="token punctuation">(</span>String dname<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>dname <span class="token operator">=</span> dname<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column dept.LOC
     *
     * @return the value of dept.LOC
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> String <span class="token function">getLoc</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> loc<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column dept.LOC
     *
     * @param loc the value for dept.LOC
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setLoc</span><span class="token punctuation">(</span>String loc<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>loc <span class="token operator">=</span> loc<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<p><code>Emp.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>bean<span class="token punctuation">;</span>

<span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Date<span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Emp</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">/**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column emp.EMPNO
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">private</span> Integer empno<span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column emp.ENAME
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">private</span> String ename<span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column emp.JOB
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">private</span> String job<span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column emp.MGR
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">private</span> Double mgr<span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column emp.HIREDATE
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">private</span> Date hiredate<span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column emp.SAL
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">private</span> Double sal<span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column emp.COMM
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">private</span> Double comm<span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column emp.DEPTNO
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">private</span> Integer deptno<span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column emp.EMPNO
     *
     * @return the value of emp.EMPNO
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> Integer <span class="token function">getEmpno</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> empno<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column emp.EMPNO
     *
     * @param empno the value for emp.EMPNO
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setEmpno</span><span class="token punctuation">(</span>Integer empno<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>empno <span class="token operator">=</span> empno<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column emp.ENAME
     *
     * @return the value of emp.ENAME
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> String <span class="token function">getEname</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> ename<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column emp.ENAME
     *
     * @param ename the value for emp.ENAME
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setEname</span><span class="token punctuation">(</span>String ename<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>ename <span class="token operator">=</span> ename<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column emp.JOB
     *
     * @return the value of emp.JOB
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> String <span class="token function">getJob</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> job<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column emp.JOB
     *
     * @param job the value for emp.JOB
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setJob</span><span class="token punctuation">(</span>String job<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>job <span class="token operator">=</span> job<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column emp.MGR
     *
     * @return the value of emp.MGR
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> Double <span class="token function">getMgr</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> mgr<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column emp.MGR
     *
     * @param mgr the value for emp.MGR
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setMgr</span><span class="token punctuation">(</span>Double mgr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>mgr <span class="token operator">=</span> mgr<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column emp.HIREDATE
     *
     * @return the value of emp.HIREDATE
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> Date <span class="token function">getHiredate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> hiredate<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column emp.HIREDATE
     *
     * @param hiredate the value for emp.HIREDATE
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setHiredate</span><span class="token punctuation">(</span>Date hiredate<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>hiredate <span class="token operator">=</span> hiredate<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column emp.SAL
     *
     * @return the value of emp.SAL
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> Double <span class="token function">getSal</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> sal<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column emp.SAL
     *
     * @param sal the value for emp.SAL
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setSal</span><span class="token punctuation">(</span>Double sal<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>sal <span class="token operator">=</span> sal<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column emp.COMM
     *
     * @return the value of emp.COMM
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> Double <span class="token function">getComm</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> comm<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column emp.COMM
     *
     * @param comm the value for emp.COMM
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setComm</span><span class="token punctuation">(</span>Double comm<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>comm <span class="token operator">=</span> comm<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column emp.DEPTNO
     *
     * @return the value of emp.DEPTNO
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> Integer <span class="token function">getDeptno</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> deptno<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column emp.DEPTNO
     *
     * @param deptno the value for emp.DEPTNO
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setDeptno</span><span class="token punctuation">(</span>Integer deptno<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>deptno <span class="token operator">=</span> deptno<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">"Emp{"</span> <span class="token operator">+</span>
                <span class="token string">"empno="</span> <span class="token operator">+</span> empno <span class="token operator">+</span>
                <span class="token string">", ename='"</span> <span class="token operator">+</span> ename <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span>
                <span class="token string">", job='"</span> <span class="token operator">+</span> job <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span>
                <span class="token string">", mgr="</span> <span class="token operator">+</span> mgr <span class="token operator">+</span>
                <span class="token string">", hiredate="</span> <span class="token operator">+</span> hiredate <span class="token operator">+</span>
                <span class="token string">", sal="</span> <span class="token operator">+</span> sal <span class="token operator">+</span>
                <span class="token string">", comm="</span> <span class="token operator">+</span> comm <span class="token operator">+</span>
                <span class="token string">", deptno="</span> <span class="token operator">+</span> deptno <span class="token operator">+</span>
                <span class="token string">'}'</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<p><code>DeptMapper.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>dao<span class="token punctuation">;</span>

<span class="token keyword">import</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>bean<span class="token punctuation">.</span>Dept<span class="token punctuation">;</span>
<span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>List<span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">DeptMapper</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table dept
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">int</span> <span class="token function">deleteByPrimaryKey</span><span class="token punctuation">(</span>Integer deptno<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table dept
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">int</span> <span class="token function">insert</span><span class="token punctuation">(</span>Dept record<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table dept
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    Dept <span class="token function">selectByPrimaryKey</span><span class="token punctuation">(</span>Integer deptno<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table dept
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    List<span class="token operator">&lt;</span>Dept<span class="token operator">></span> <span class="token function">selectAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table dept
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">int</span> <span class="token function">updateByPrimaryKey</span><span class="token punctuation">(</span>Dept record<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p><code>EmpMapper.class</code></p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>dao<span class="token punctuation">;</span>

<span class="token keyword">import</span> com<span class="token punctuation">.</span>cmz<span class="token punctuation">.</span>bean<span class="token punctuation">.</span>Emp<span class="token punctuation">;</span>
<span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>List<span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">EmpMapper</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table emp
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">int</span> <span class="token function">deleteByPrimaryKey</span><span class="token punctuation">(</span>Integer empno<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table emp
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">int</span> <span class="token function">insert</span><span class="token punctuation">(</span>Emp record<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table emp
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    Emp <span class="token function">selectByPrimaryKey</span><span class="token punctuation">(</span>Integer empno<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table emp
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    List<span class="token operator">&lt;</span>Emp<span class="token operator">></span> <span class="token function">selectAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table emp
     *
     * @mbg.generated Sat Apr 18 00:28:30 CST 2020
     */</span>
    <span class="token keyword">int</span> <span class="token function">updateByPrimaryKey</span><span class="token punctuation">(</span>Emp record<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p><code>DeptMapper.xml</code></p>
<pre class=" language-xml"><code class="language-xml"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8"?></span>
<span class="token doctype">&lt;!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>mapper</span> <span class="token attr-name">namespace</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.dao.DeptMapper<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>resultMap</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>BaseResultMap<span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Dept<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat Apr 18 00:28:30 CST 2020.
    --></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>DEPTNO<span class="token punctuation">"</span></span> <span class="token attr-name">jdbcType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>INTEGER<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>DNAME<span class="token punctuation">"</span></span> <span class="token attr-name">jdbcType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>VARCHAR<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dname<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>LOC<span class="token punctuation">"</span></span> <span class="token attr-name">jdbcType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>VARCHAR<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>loc<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>resultMap</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>delete</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deleteByPrimaryKey<span class="token punctuation">"</span></span> <span class="token attr-name">parameterType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>java.lang.Integer<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat Apr 18 00:28:30 CST 2020.
    --></span>
    delete from dept
    where DEPTNO = #{deptno,jdbcType=INTEGER}
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>delete</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>insert</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>insert<span class="token punctuation">"</span></span> <span class="token attr-name">parameterType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Dept<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat Apr 18 00:28:30 CST 2020.
    --></span>
    insert into dept (DEPTNO, DNAME, LOC
      )
    values (#{deptno,jdbcType=INTEGER}, #{dname,jdbcType=VARCHAR}, #{loc,jdbcType=VARCHAR}
      )
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>insert</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>update</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>updateByPrimaryKey<span class="token punctuation">"</span></span> <span class="token attr-name">parameterType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Dept<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat Apr 18 00:28:30 CST 2020.
    --></span>
    update dept
    set DNAME = #{dname,jdbcType=VARCHAR},
      LOC = #{loc,jdbcType=VARCHAR}
    where DEPTNO = #{deptno,jdbcType=INTEGER}
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>update</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectByPrimaryKey<span class="token punctuation">"</span></span> <span class="token attr-name">parameterType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>java.lang.Integer<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>BaseResultMap<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat Apr 18 00:28:30 CST 2020.
    --></span>
    select DEPTNO, DNAME, LOC
    from dept
    where DEPTNO = #{deptno,jdbcType=INTEGER}
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectAll<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>BaseResultMap<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat Apr 18 00:28:30 CST 2020.
    --></span>
    select DEPTNO, DNAME, LOC
    from dept
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>mapper</span><span class="token punctuation">></span></span></code></pre>
<p><code>EmpMapper.xml</code></p>
<pre class=" language-xml"><code class="language-xml"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8"?></span>
<span class="token doctype">&lt;!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>mapper</span> <span class="token attr-name">namespace</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.dao.EmpMapper<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>resultMap</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>BaseResultMap<span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat Apr 18 00:28:30 CST 2020.
    --></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>EMPNO<span class="token punctuation">"</span></span> <span class="token attr-name">jdbcType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>INTEGER<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>empno<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ENAME<span class="token punctuation">"</span></span> <span class="token attr-name">jdbcType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>VARCHAR<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ename<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>JOB<span class="token punctuation">"</span></span> <span class="token attr-name">jdbcType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>VARCHAR<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>job<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>MGR<span class="token punctuation">"</span></span> <span class="token attr-name">jdbcType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>DOUBLE<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>mgr<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>HIREDATE<span class="token punctuation">"</span></span> <span class="token attr-name">jdbcType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>DATE<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>hiredate<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>SAL<span class="token punctuation">"</span></span> <span class="token attr-name">jdbcType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>DOUBLE<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sal<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>COMM<span class="token punctuation">"</span></span> <span class="token attr-name">jdbcType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>DOUBLE<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>comm<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>DEPTNO<span class="token punctuation">"</span></span> <span class="token attr-name">jdbcType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>INTEGER<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deptno<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>resultMap</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>delete</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>deleteByPrimaryKey<span class="token punctuation">"</span></span> <span class="token attr-name">parameterType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>java.lang.Integer<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat Apr 18 00:28:30 CST 2020.
    --></span>
    delete from emp
    where EMPNO = #{empno,jdbcType=INTEGER}
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>delete</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>insert</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>insert<span class="token punctuation">"</span></span> <span class="token attr-name">parameterType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat Apr 18 00:28:30 CST 2020.
    --></span>
    insert into emp (EMPNO, ENAME, JOB, 
      MGR, HIREDATE, SAL, COMM, 
      DEPTNO)
    values (#{empno,jdbcType=INTEGER}, #{ename,jdbcType=VARCHAR}, #{job,jdbcType=VARCHAR}, 
      #{mgr,jdbcType=DOUBLE}, #{hiredate,jdbcType=DATE}, #{sal,jdbcType=DOUBLE}, #{comm,jdbcType=DOUBLE}, 
      #{deptno,jdbcType=INTEGER})
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>insert</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>update</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>updateByPrimaryKey<span class="token punctuation">"</span></span> <span class="token attr-name">parameterType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.cmz.bean.Emp<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat Apr 18 00:28:30 CST 2020.
    --></span>
    update emp
    set ENAME = #{ename,jdbcType=VARCHAR},
      JOB = #{job,jdbcType=VARCHAR},
      MGR = #{mgr,jdbcType=DOUBLE},
      HIREDATE = #{hiredate,jdbcType=DATE},
      SAL = #{sal,jdbcType=DOUBLE},
      COMM = #{comm,jdbcType=DOUBLE},
      DEPTNO = #{deptno,jdbcType=INTEGER}
    where EMPNO = #{empno,jdbcType=INTEGER}
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>update</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectByPrimaryKey<span class="token punctuation">"</span></span> <span class="token attr-name">parameterType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>java.lang.Integer<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>BaseResultMap<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat Apr 18 00:28:30 CST 2020.
    --></span>
    select EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
    from emp
    where EMPNO = #{empno,jdbcType=INTEGER}
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>selectAll<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>BaseResultMap<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token comment" spellcheck="true">&lt;!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat Apr 18 00:28:30 CST 2020.
    --></span>
    select EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
    from emp
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>select</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>mapper</span><span class="token punctuation">></span></span></code></pre>
<h3 id="5-2-MyBatis3DynamicSql"><a href="#5-2-MyBatis3DynamicSql" class="headerlink" title="5.2 MyBatis3DynamicSql"></a>5.2 MyBatis3DynamicSql</h3><p>次方法只能生成注解不能生成xml</p>
<p><img src="C:%5CUsers%5Ccmz%5CDesktop%5Cmybatis%5Cupload%5Cimage-20200418004940920.png" alt="MyBatis3DynamicSql"></p>

            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://caimengzhi.gitee.io" rel="external nofollow noreferrer">夏天</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://caimengzhi.gitee.io/2020/04/10/mybatis/mybatis-xmlinfo/">https://caimengzhi.gitee.io/2020/04/10/mybatis/mybatis-xmlinfo/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="https://caimengzhi.gitee.io" target="_blank">夏天</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/mybatis/">
                                    <span class="chip bg-color">mybatis</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
    #love{
       color:red; 
    }
    #page_over{
       height: 38px;
       text-align:center;
       font-size: 20px;
       line-height: 38px;
       background-color: #8FDFDB; 
    }
</style>

<div id="reward">
    <div id="page_over">--------本文结束感谢您的阅读!---------</div>
    <h4><span id="love">❤</span></span>如果文章能够帮到您的话，不妨就支持一下小编呗!<span id="love">❤</span></h4>
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>
            
        </div>
    </div>

    

    

    

    
    <div class="livere-card card" data-aos="fade-up">
    <!-- 来必力City版安装代码 -->
    <div id="lv-container" class="card-content" data-id="city" data-uid="MTAyMC80OTUxNS8yNjAwNgs">
        <script type="text/javascript">
            (function (d, s) {
                let j, e = d.getElementsByTagName(s)[0];
                if (typeof LivereTower === 'function') {
                    return;
                }

                j = d.createElement(s);
                j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
                j.async = true;

                e.parentNode.insertBefore(j, e);
            })(document, 'script');
        </script>
        <noscript>为正常使用来必力评论功能请激活JavaScript。</noscript>
    </div>
    <!-- City版安装代码已完成 -->
</div>
    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2020/04/13/scratch/scrath-basic/">
                    <div class="card-image">
                        
                        <img src="https://gitee.com/caimengzhi/picBlog/raw/master/scratch.png" class="responsive-img" alt="scratch基础">
                        
                        <span class="card-title">scratch基础</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            Scratch基本功能介绍、使用。
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2020-04-13
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E5%B0%91%E5%84%BF%E7%BC%96%E7%A8%8B/" class="post-category">
                                    少儿编程
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/%E5%B0%91%E5%84%BF%E7%BC%96%E7%A8%8B/">
                        <span class="chip bg-color">少儿编程</span>
                    </a>
                    
                    <a href="/tags/scratch/">
                        <span class="chip bg-color">scratch</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2020/04/10/scratch/scrath-info/">
                    <div class="card-image">
                        
                        <img src="https://gitee.com/caimengzhi/picBlog/raw/master/scratch.png" class="responsive-img" alt="scratch简介">
                        
                        <span class="card-title">scratch简介</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            Scratch是图形化编程工具
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-04-10
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E5%B0%91%E5%84%BF%E7%BC%96%E7%A8%8B/" class="post-category">
                                    少儿编程
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/%E5%B0%91%E5%84%BF%E7%BC%96%E7%A8%8B/">
                        <span class="chip bg-color">少儿编程</span>
                    </a>
                    
                    <a href="/tags/scratch/">
                        <span class="chip bg-color">scratch</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


<!-- 代码块折行 -->

<style type="text/css">
code[class*="language-"], pre[class*="language-"] { white-space: pre !important; }
</style>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>



    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="503838841"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>
    
    <div class="container row center-align" style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            <span id="year">2019</span>
            <a href="https://caimengzhi.gitee.io" target="_blank">夏天</a>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">39.7k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
           
        
            
            <span id="sitetime">载入运行时间...</span>
            <script>
                function siteTime() {
                    var seconds = 1000;
                    var minutes = seconds * 60;
                    var hours = minutes * 60;
                    var days = hours * 24;
                    var years = days * 365;
                    var today = new Date();
                    var startYear = "2019";
                    var startMonth = "6";
                    var startDate = "28";
                    var startHour = "0";
                    var startMinute = "0";
                    var startSecond = "0";
                    var todayYear = today.getFullYear();
                    var todayMonth = today.getMonth() + 1;
                    var todayDate = today.getDate();
                    var todayHour = today.getHours();
                    var todayMinute = today.getMinutes();
                    var todaySecond = today.getSeconds();
                    var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                    var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                    var diff = t2 - t1;
                    var diffYears = Math.floor(diff / years);
                    var diffDays = Math.floor((diff / days) - diffYears * 365);
                    var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
                    var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) /
                        minutes);
                    var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
                        diffMinutes * minutes) / seconds);
                    if (startYear == todayYear) {
                        document.getElementById("year").innerHTML = todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffDays + " 天 " + diffHours +
                            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    } else {
                        document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffYears + " 年 " + diffDays +
                            " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    }
                }
                setInterval(siteTime, 1000);
            </script>
            
            <br>
            <script src="https://pv.sohu.com/cityjson?ie=utf-8"></script> 
            <script type="text/javascript">  
                document.write('欢迎来自:' + returnCitySN["cname"]+'的朋友' + ",您的IP:" + returnCitySN["cip"] + ",浏览器版本:"+getBrowserInfo());
                function getBrowserInfo()
                {
                    var agent = navigator.userAgent.toLowerCase() ;
                    var regStr_ie = /msie [\d.]+;/gi ;
                    var regStr_ff = /firefox\/[\d.]+/gi
                    var regStr_chrome = /chrome\/[\d.]+/gi ;
                    var regStr_saf = /safari\/[\d.]+/gi ;
                    //IE
                    if(agent.indexOf("msie") > 0)
                    {
                        return agent.match(regStr_ie) ;
                    }
                    //firefox
                    if(agent.indexOf("firefox") > 0)
                    {
                        return agent.match(regStr_ff) ;
                    }
                    //Chrome
                    if(agent.indexOf("chrome") > 0)
                    {
                        return agent.match(regStr_chrome) ;
                    }
                    //Safari
                    if(agent.indexOf("safari") > 0 && agent.indexOf("chrome") < 0)
                    {
                        return agent.match(regStr_saf) ;
                    }
                }
            </script> 
        
            
            <span id="icp"><img src="/medias/icp.png" style="vertical-align: text-bottom;" />
                <a href="https://caimengzhi.gitee.io/books/" target="_blank">爱一直在线</a>
            </span>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://caimengzhi.gitee.io/books/" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:610658552@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=QQ 联系我 610658552" class="tooltipped" target="_blank" data-tooltip="QQ联系我: QQ 联系我 610658552" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>





    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    
   
    

    
    
    <script>
        (function (i, s, o, g, r, a, m) {
            i["DaoVoiceObject"] = r;
            i[r] = i[r] || function () {
                (i[r].q = i[r].q || []).push(arguments)
            }, i[r].l = 1 * new Date();
            a = s.createElement(o), m = s.getElementsByTagName(o)[0];
            a.async = 1;
            a.src = g;
            a.charset = "utf-8";
            m.parentNode.insertBefore(a, m)
        })(window, document, "script", ('https:' == document.location.protocol ? 'https:' : 'http:') +
            "//widget.daovoice.io/widget/6984b559.js", "daovoice")
        daovoice('init', {
            app_id: "29275150"
        });
        daovoice('update');
    </script>
    

    
    <script type="text/javascript" color="0,0,255"
        pointColor="0,0,255" opacity='0.7'
        zIndex="-1" count="99"
        src="/libs/background/canvas-nest.js"></script>
    

    

    
    <script type="text/javascript" src="/libs/background/ribbon-dynamic.js" async="async"></script>
    
    
    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    
    <!-- caimengzhi 
    <div id="weather-view-he"></div>
    -->
    
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>

</html>

<script type="text/javascript">
//只在桌面版网页启用特效
var windowWidth = $(window).width();
if (windowWidth > 768) {
    document.write('<script type="text/javascript" src="/js/sakura.js"><\/script>');
}
</script>


<!--<script type="text/javascript" src="/js/click_show_text.js"></script>-->
<script type="text/javascript" src="/js/FunnyTitle.js"></script>

<canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas> 
<script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
<!--<script type="text/javascript" src="/js/fireworks.js"></script>-->
<script type="text/javascript" src="/js/cursor.js"></script>

<!-- summer -->
<script type="text/javascript">
WIDGET = {FID: '5bDQrVS13z'}
</script>
<script type="text/javascript" src="https://apip.weatherdt.com/float/static/js/r.js?v=1111"></script>